如何在iOS的cocoapod库中使用图像资产目录 [英] How to use images asset catalog in cocoapod library for iOS

查看:1324
本文介绍了如何在iOS的cocoapod库中使用图像资产目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cocoapod库,其中包含2种格式的资产:

I have a cocoapod library which contains assets in 2 formats:


  • a .storyboard

  • XCode资产目录.xcassets(带图像)

我的 podspec 文件包含资源包的定义:

my podspec file contains the definition for the resource bundle:

s.resource_bundle = {'SparkSetup' => ['Resources/**/*.{xcassets,storyboard}']}

我有一个pod项目中的单独目标通过使用这些文件+该包的plist文件来创建资源包。

and I have a separate target in the pod project to create a resource bundle by using those files + a plist file for that bundle.

当我在应用程序项目中使用pod时 - 我可以看到故事板/ xcassets文件位于pod目标中,我可以轻松访问和运行故事板,但故事板中引用的图像(到.xcassets文件)在运行时找不到(但在IB中正确显示) )。

thing is that when I use the pod in an app project - I can see the storyboard/xcassets files being in the pod target and I can access and run the storyboard easily but the images referenced in the storyboard (to the .xcassets file) are not found in run-time (but displayed correctly in IB).

我得到的错误是:

Could not load the "spinner" image referenced from a nib in the bundle with identifier "(null)"

我确实看到了产品目录中的捆绑文件。
要在故事板中实现风险投资,我使用:

I do see a bundle file in the products directory. To instanciate VCs in the storyboard I use:

+(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;
}

这似乎很适合查找故事板,但不适合查找图片同一捆绑中的.xcassets。

which seems to work well for finding the storyboard, but not for finding images in the .xcassets in the same bundle.

我做错了什么?如何从这个故事板/代码中引用图像,并能够将此UI pod集成到任何应用程序中?

What am I doing wrong? how can I reference images from this storyboard/from code and be able to integrate this UI pod into any app?

谢谢!

推荐答案

至少从Cocoapods版本1.0.1开始,支持图像资产目录。

At least as of version 1.0.1 of Cocoapods, image asset catalogs are supported.

在我的Swift中代码,我用过:

In my Swift code, I used:

public static var GoogleIcon:UIImage {
    let bundle = NSBundle(forClass: self)
    Log.msg("bundle: \(bundle)")
    return UIImage(named: "GoogleIcon", inBundle: bundle,compatibleWithTraitCollection: nil)!
}

在我的pod规范中,我使用了:

In my pod spec, I used:

s.resources = "SMCoreLib/Assets/*.xcassets"

当我使用模拟器构建时,.car文件 显示在框架中:

When I build using the simulator, the .car file does show up in the Framework:

cd /Users/<snip>/SMCoreLib.framework 
ls
Assets.car  Info.plist  SMCoreLib

这篇关于如何在iOS的cocoapod库中使用图像资产目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆