ios – 访问pod中的资源

我想将图像资源包含在一个Cocapod库中,但是我无法访问它们.我已经阅读了这些资源寻求帮助:

Cocoapods Resources

Cocoapods Resource Bundles

Mokacoding Resource Bundles

David Potter Resource Bundles

但是,我没有指出访问资源的方向.我尝试过以下操作:

[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets"] URLForResource:@"my-image@2x" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets"] URLForResource:@"my-image" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets.bundle"] URLForResource:@"my-image" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@"my-image" withExtension:@"png"]]]
[[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@"my-image@2x" withExtension:@"png"]]]
[UIImage imageNamed:@"my-asset"]
[UIImage imageNamed:@"[email protected]"]

但没有一个工作.

我已经使用这些替代方案设置了我的podspec,并且不能与上述配合使用:

s.resources = ['Pod/Assets/*.{png,jpg}','Pod/Assets/**/*.{png,jpg}']
s.resource_bundles = {
    'Assets' => ['Pod/Assets/*.{png,jpg}']
}

资源显示在“开发荚/我的应用程序/资源”后,更新后,但我无法访问他们的生活.没有任何捆绑,没有任何名为“资产”的东西.

任何帮助或指导将不胜感激.

解决方法

这取决于您使用的Cocoapods版本.使用旧版本的Cocapods,您可以使用[NSBundle mainBundle]:pathForResource:ofType:
Nsstring *bundlePath = [[NSBundle mainBundle] 
    pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

如果您在podspec文件中使用较新的Cocoapods并使用spec.resource_bundles而不是spec.resources,则必须避免使用mainBundle并将其替换为NSBundle bundleForClass,“Returns the NSBundle object with which the specified class is associated

例:

Nsstring *bundlePath = [[NSBundle bundleForClass:[MyFrameworkClass class]] 
    pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...