min*_*omb 27 iphone xcode objective-c ios cocoapods
我有一个cocoapod库,其中包含2种格式的资产:
我的podspec文件包含资源包的定义:
s.resource_bundle = {'SparkSetup' => ['Resources/**/*.{xcassets,storyboard}']}
Run Code Online (Sandbox Code Playgroud)
我在pod项目中有一个单独的目标,通过使用这些文件+该包的plist文件来创建资源包.
事情是,当我在应用程序项目中使用pod时 - 我可以看到storyboard/xcassets文件位于pod目标中,我可以轻松访问和运行故事板,但故事板中引用的图像(到.xcassets文件)是在运行时未找到(但在IB中正确显示).
我得到的错误是:
Could not load the "spinner" image referenced from a nib in the bundle with identifier "(null)"
Run Code Online (Sandbox Code Playgroud)
我确实在products目录中看到了一个bundle文件.要在故事板中实现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;
}
Run Code Online (Sandbox Code Playgroud)
这似乎很适合找到故事板,但不适用于在同一捆绑中的.xcassets中查找图像.
我究竟做错了什么?如何从这个故事板/代码中引用图像,并能够将此UI pod集成到任何应用程序中?
谢谢!
Chr*_*nce 33
至少从Cocoapods 1.0.1版开始,支持图像资产目录.
在我的Swift代码中,我使用了:
public static var GoogleIcon: UIImage {
let bundle = Bundle(for: self)
log.msg("bundle: \(bundle)")
return UIImage(named: "GoogleIcon", in: bundle, compatibleWith: nil)!
}
Run Code Online (Sandbox Code Playgroud)
在我的pod规范中,我使用了:
s.resources = "SMCoreLib/Assets/*.xcassets"
Run Code Online (Sandbox Code Playgroud)
当我使用模拟器构建时,.car文件确实显示在框架中:
cd /Users/<snip>/SMCoreLib.framework
ls
Assets.car Info.plist SMCoreLib
Run Code Online (Sandbox Code Playgroud)
min*_*omb 11
好吧,通过pod不支持图像资产目录 - 只需在podspec中包含一个包含图像文件的资源包,如下所示:
s.subspec 'Resources' do |resources|
resources.resource_bundle = {'MyBundle' => ['Resources/**/*.{png}']}
end
Run Code Online (Sandbox Code Playgroud)
并安全地从pod中访问图像:
+(NSBundle *)getResourcesBundle
{
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"MyBundle" withExtension:@"bundle"]];
return bundle;
}
+(UIImage *)loadImageFromResourceBundle:(NSString *)imageName
{
NSBundle *bundle = [MyClass getResourcesBundle];
NSString *imageFileName = [NSString stringWithFormat:@"%@.png",imageName];
UIImage *image = [UIImage imageNamed:imageFileName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
Run Code Online (Sandbox Code Playgroud)
这解决了在cocoapod中处理图像/资源的所有问题.
如果您使用的是 swift。
class func loadImage(name: String) -> UIImage? {
let podBundle = NSBundle(forClass: MyClass.self)
if let url = podBundle.URLForResource("MyBundleName", withExtension: "bundle") {
let bundle = NSBundle(URL: url)
return UIImage(named: name, inBundle: bundle, compatibleWithTraitCollection: nil)
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3版本
private func getImageFromBundle(name: String) -> UIImage {
let podBundle = Bundle(for: YourClass.self)
if let url = podBundle.url(forResource: "YourClass", withExtension: "bundle") {
let bundle = Bundle(url: url)
return UIImage(named: name, in: bundle, compatibleWith: nil)!
}
return UIImage()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13742 次 |
| 最近记录: |