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:@"my-asset@2x.png"]

但没有一个工作.

我已经使用这些替代方案设置了我的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];

以上是来客网为你收集整理的ios – 访问pod中的资源全部内容,希望文章能够帮你解决ios – 访问pod中的资源所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。