我正在创建一个私有库,其中包含库中组件引用的资源.该图书馆与使用CocoaPods的应用程序共享.在图书馆的.podspec中,我已经添加了一行:
s.resource_bundles = {'IXMapKitResources' => ['IXMapKit/Resources/*']}
包中的一个资源是具有多个映像集的资产目录,其中一个称为“IXMKAnnotationIcons-normal-Accident”.以下代码返回一个零:
UIImage * image = [UIImage imageNamed: @"IXMKAnnotationIcons-normal-Accident"];
我发现一个article on mokacoding.com描述如何从一个pod加载字体,但这对我来说没有效果:
- (UIImage *) ixmk_imageNamed: (Nsstring *) name { Nsstring * IXMKResourceBundleName = @"IXMapKitResources.bundle"; Nsstring * resourceName = [Nsstring stringWithFormat: @"%@/%@",IXMKResourceBundleName,name]; Nsstring * imageTypestring = [self ixmk_stringForImageType: imageType]; NSURL * url = [[NSBundle mainBundle] URLForResource: resourceName withExtension: imageTypestring]; NSData * imageData = [NSData dataWithContentsOfURL: url]; if (imageData != nil) { CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) imageData); CGImageRef imageRef = [self ixmk_imageFromDataProvider: provider imageType: imageType]; UIImage * image = [UIImage imageWithCGImage: imageRef]; CFRelease(imageRef); CFRelease(provider); return image; } else { return nil; } }
CocoaPods webpage describing the resources keyword有以下警告:
We strongly recommend library developers to adopt resource bundles as
there can be name collisions using the resources attribute. Moreover
resources specified with this attribute are copied directly to the
client target and therefore they are not optimized by Xcode.
我在这里做什么是失败的.
解决方法
这可能是资产目录的问题,而不是CocoaPods或bundle.将图像从资产目录中移出可以解决问题. Apple似乎不支持二级资源包中的资产目录.可怜.