ios - 如何在 iOS 的 cocoapod 库中使用图像 Assets 目录

标签 ios objective-c iphone xcode cocoapods

我有一个 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/

相关文章:

ios - 当按下 UIButton 并调用 IBAction 时,显示带有事件指示器的 UIAlertView 直到完成

ios - 如何从NSIndexSet筛选和创建范围?

iphone - NSRegularExpression 提取文本

ios - 从我的 iphone 画廊上传照片

ios - 如何在 Swift 的 UI 上呈现异步调用提供的数据?

ios - 如何使用 Objective-C 将字符添加到 iOS 中的动画 UILabel 中?

iphone - 通话中状态栏如何影响 ViewController View 大小?

iphone - 如何在文本字段中临时保存文本?

ios - 如何使用 UIWebView 制作浏览器?

ios - 如何在 iOS 中实现 SSL Pinning?