从CocoaPods资源包加载图像

我正在创build一个私有库,其中包含库中组件引用的资源。 该库与使用CocoaPods的应用程序共享。 在库的.podspec中,我已经包含了这样一行:

s.resource_bundles = {'IXMapKitResources' => ['IXMapKit/Resources/*']} 

捆绑中的资源之一是具有多个图像集的资产目录,其中之一被称为“IXMKAnnotationIcons-Normal-Accident”。 以下代码返回一个零:

 UIImage * image = [UIImage imageNamed: @"IXMKAnnotationIcons-Normal-Accident"]; 

我在mokacoding.com上find了一篇关于如何从一个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网页有以下警告:

我们强烈build议图书馆开发人员采用资源包,因为可能会使用资源属性进行名称冲突。 此外,使用此属性指定的资源直接复制到客户端目标,因此它们不会被Xcode优化。

我在这里失去了什么。

这是资产目录的问题,而不是CocoaPods或捆绑包。 将图像移出资产目录解决了这个问题。 看起来像Apple不支持辅助资源包中的资产目录。 可怜。

没有在任何地方列出的明显的解决scheme是使用Cocoapods生成的束(该文件夹实际上不存在)作为NSBundle:

试试看: http : //blog.flaviocaetano.com/post/cocoapods-and-resource_bundles/

它为我工作!

例如派拉蒙

podspec

 s.resource_bundle = { 'Paramount' => ['Sources/Paramount.bundle/*.png'] } 

swift

 public extension UIImage { static func make(name: String) -> UIImage? { let bundle = NSBundle(forClass: Paramount.Toolbar.self) return UIImage(named: "Paramount.bundle/\(name)", inBundle: bundle, compatibleWithTraitCollection: nil) } } 

这里Paramount.Toolbar.self可以是该框架中的任何类

至于我,我不得不将“s.resources”添加到.podspec。 在这之前,

只有s.resource_bundles是这样设置的:'BPPicker / Assets / ',将其更改为“BPPicker / Assets / *”。

 # # Be sure to run `pod lib lint BPPicker.podspec' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| s.name = 'BPPicker' s.version = '0.1.11' s.summary = 'This is the imito Body Part Picker.' # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? # * Try to keep it short, snappy and to the point. # * Write the description between the DESC delimiters below. # * Finally, don't worry about the indent, CocoaPods strips it! s.description = <<-DESC TODO: Add long description of the pod here. DESC s.homepage = 'https://bitbucket.org/imito/bppicker' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'Ievgen Naloiko' => 'naloiko@gmail.com' } s.source = { :git => 'https://ievgen_naloiko@bitbucket.org/imito/bppicker.git', :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' s.ios.deployment_target = '9.0' s.source_files = 'BPPicker/Classes/**/*' s.resource_bundles = { 'BPPicker' => ['BPPicker/Assets/*'] } s.resources = "BPPicker/**/*.{png,json}" s.frameworks = 'UIKit' s.dependency 'ObjectMapper' end 

我做了一个可以在pod中find资源包的pod。 我希望一劳永逸地结束“CocoaPods资源包”的问题。

https://github.com/haifengkao/PodAsset