我有一个 cocoapod 库,其中包含两种格式的 Assets :
- 一个 Storyboard
- XCode Assets 目录 .xcassets(带有图像)
我的 podspec
文件包含资源包的定义:
s.resource_bundle = {'SparkSetup' => ['Resources/**/*.{xcassets,storyboard}']}
并且我在 pod 项目中有一个单独的目标,用于通过使用这些文件 + 该资源包的 plist 文件来创建资源包。
事情是,当我在应用程序项目中使用 pod 时 - 我可以看到 pod 目标中的 Storyboard/xcassets 文件,我可以轻松访问和运行 Storyboard,但 Storyboard中引用的图像(到 .xcassets文件)在运行时找不到(但在 IB 中正确显示)。
我得到的错误是:
Could not load the "spinner" image referenced from a nib in the bundle with identifier "(null)"
我确实在产品目录中看到了一个捆绑文件。 要在 Storyboard中实例化 VC,我使用:
+(NSBundle *)getResourcesBundle
{
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"SparkSetup" withExtension:@"bundle"]];
return bundle;
}
+(UIStoryboard *)getSetupStoryboard
{
UIStoryboard *setupStoryboard = [UIStoryboard storyboardWithName:@"setup" bundle:[SparkSetupMainController getResourcesBundle]];
return setupStoryboard;
}
这似乎适用于查找 Storyboard,但不适用于在同一包中的 .xcassets 中查找图像。
我做错了什么?我如何从这个 Storyboard/代码中引用图像并能够将这个 UI pod 集成到任何应用程序中?
谢谢!
最佳答案
至少从 Cocoapods 1.0.1 版本开始,支持图像 Assets 目录。
在我的 Swift 4.2 代码中,我使用了:
public static var GoogleIcon: UIImage {
let bundle = Bundle(for: self)
log.msg("bundle: \(bundle)")
return UIImage(named: "GoogleIcon", in: bundle, compatibleWith: nil)!
}
在我的 pod 规范中,我使用了:
s.resources = "SMCoreLib/Assets/*.xcassets"
当我使用模拟器构建时,.car 文件确实出现在框架中:
cd /Users/<snip>/SMCoreLib.framework
ls
Assets.car Info.plist SMCoreLib
关于ios - 如何在 iOS 的 cocoapod 库中使用图像 Assets 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32577227/