我正在创建一个私有库,其中包含库中组件引用的资源。该库与使用CocoaPods的应用程序共享。在库的.podspec中,我包含了这样一行:
s.resource_bundles = {'IXMapKitResources' => ['IXMapKit/Resources/*']}
捆绑包中的一个资源是具有多个图像集的资产目录,其中一个称为“IXMKAnnotationIcons-Normal-Accident”。以下代码返回nil:
UIImage * image = [UIImage imageNamed: @"IXMKAnnotationIcons-Normal-Accident"];
我找到了一个描述如何从pod加载字体的article on mokacoding.com,但这对我不起作用:
- (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有以下警告:
我们强烈建议库开发人员采用资源包作为 可以使用resources属性进行名称冲突。此外 使用此属性指定的资源将直接复制到 客户端目标,因此它们没有被Xcode优化。
我不知道该怎么做。
答案 0 :(得分:12)
这证明是资产目录的问题,而不是CocoaPods或bundle。将图像移出资产目录可以解决问题。看起来Apple不支持辅助资源包中的资产目录。可惜。
答案 1 :(得分:5)
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
可以是该框架中的任何类
答案 2 :(得分:2)
至于我,我不得不将“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
答案 3 :(得分:0)
我制作了一个可以在pod中找到资源包的pod。 我希望它能结束&#34; CocoaPods资源包&#34;问题一劳永逸。
答案 4 :(得分:0)
此答案适用于 Swift3 和 Swift4 。
在pod目录下创建一个函数到文件中。
func loadImageBundle(named imageName:String) -> UIImage? {
let podBundle = Bundle(for: self.classForCoder)
if let bundleURL = podBundle.url(forResource: "Update with directory name", withExtension: "bundle")
{
let imageBundel = Bundle(url:bundleURL )
let image = UIImage(named: imageName, in: imageBundel, compatibleWith: nil)
return image
}
return nil
}
用法
imageView.image = loadImageBundle(named: "imagefile")
答案 5 :(得分:0)
在您的pod文件中:images are in .xcassets so it can be easily organized with 1x, 2x, 3x
s.resources = 'PodName/Assets/**'
用法:Swift
let bundle = Bundle(for: ClassThatWillUseTheImage.self)
let image = UIImage(named: "close", in: bundle, with: .none)
用法:Objective-C
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:@"close" inBundle:bundle withConfiguration:nil];
在版本11.4(11E146)iOS 13.0+中进行了测试
使用#imageLiteral(resourceName: "close")
无效,它显示正在读取的图像,但会抛出错误。