【发布时间】:2014-03-18 10:55:36
【问题描述】:
简而言之:
如何从 NSBundle 中的已编译 Assets.car 加载图像?
完整版:
我正在将一套应用程序转换为使用CocoaPods。每个应用都依赖于一个名为 Core 的共享 pod。
Core 包括代码文件、xib 文件和几个xcasset 文件。
以下是 Podspec 中用于创建资源包的 Core 的相关行:
s.resource_bundles = {'CoreResources' => ['Core/Resources/*']}
Podspec 传递 pod spec lint,依赖它的主项目正确构建。
但是,Core 中的任何xcasset 文件中没有个图像显示。
我(天真地)尝试使用UIImage 上的类别加载图像,如下所示:
@implementation UIImage (Bundle)
+ (UIImage *)imageNamed:(NSString *)name bundle:(NSBundle *)bundle
{
if (!bundle)
return [UIImage imageNamed:name];
UIImage *image = [UIImage imageNamed:[self imageName:name forBundle:bundle]];
return image;
}
+ (NSString *)imageName:(NSString *)name forBundle:(NSBundle *)bundle
{
NSString *bundleName = [[bundle bundlePath] lastPathComponent];
name = [bundleName stringByAppendingPathComponent:name];
return name;
}
@end
以前,Core 是 submodule,这个解决方案运行良好。然而,在检查我之前的bundle 文件(与main 捆绑包分开)后,我注意到所有图像都被简单地复制到bundle... 即
Image.png、Image@2x.png 等都在捆绑包中。
在检查CocoaPods-生成的包后,它包含一个
Assets.car
据我了解,它是上述Core 子目录中的xcasset 文件所有的组合编译版本。
如何在这个Core 资源包中从这个编译的Assets.car 加载图像?
作为一个黑客,我想我可以......
Podspec syntax reference 举个例子:
spec.resource = "Resources/HockeySDK.bundle"
这似乎表明可以在 Xcode 中手动创建包,然后让 CocoaPods 简单地复制它。
不过,这更像是一个hack,而不是一个解决方案。
我相信 CocoaPods (v 0.29+) 可以完全处理这个...?
【问题讨论】: