xcode 在 pod 中访问资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25817738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Resources in pod
提问by RileyE
I would like to include image assets in a cocoapod library, but I'm having trouble accessing them. I've read these resources for help:
我想在 cocoapod 库中包含图像资产,但我无法访问它们。我已阅读这些资源寻求帮助:
However, none point me in the direction of accessing the resources. I've tried the following:
但是,没有人指出我访问资源的方向。我尝试了以下方法:
[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]"]
But none of them work.
但它们都不起作用。
I've setup my podspec with these alternatives and neither work with the above:
我已经使用这些替代方法设置了我的 podspec,但都不适用于上述方法:
s.resources = ['Pod/Assets/*.{png, jpg}', 'Pod/Assets/**/*.{png, jpg}']
s.resource_bundles = {
'Assets' => ['Pod/Assets/*.{png, jpg}', 'Pod/Assets/**/*.{png, jpg}']
}
The resources show up under 'Development Pods/My App/Resources' after a pod update
, but I cannot access them for the life of me. There aren't any bundles and there isn't anything named 'Assets'.
资源显示在 'Development Pods/My App/Resources' 下pod update
,但我终生无法访问它们。没有任何捆绑包,也没有任何名为“资产”的东西。
Any help or guidance would be appreciated.
任何帮助或指导将不胜感激。
回答by Hugh
It depends on the version of Cocoapods that you use. With older versions of cocoapods, you could get away with using [NSBundle mainBundle]:pathForResource:ofType:
这取决于您使用的 Cocoapods 版本。对于旧版本的 cocoapods,你可以使用 [NSBundle mainBundle]:pathForResource:ofType:
NSString *bundlePath = [[NSBundle mainBundle]
pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
If you are using newer Cocoapods and using spec.resource_bundles instead of spec.resources in your podspec file, you have to avoid mainBundle and replace it with NSBundle bundleForClass, which "Returns the NSBundle object with which the specified class is associated"
如果您使用较新的 Cocoapods 并在 podspec 文件中使用 spec.resource_bundles 而不是 spec.resources,则必须避免使用 mainBundle 并将其替换为 NSBundle bundleForClass,它“返回与指定类关联的 NSBundle 对象”
Example:
例子:
NSString *bundlePath = [[NSBundle bundleForClass:[MyFrameworkClass class]]
pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
回答by sclamage
This tripped me up too. Maybe I'm misunderstanding +bundleWithIdentifier:
, but I don't think you can use it to get to a resource bundle inside of your iOS app bundle. See the "Getting Bundles by Identifier"section of the bundle docs for more details.
这也把我绊倒了。也许我误解了+bundleWithIdentifier:
,但我认为您不能使用它来获取 iOS 应用程序包内的资源包。有关更多详细信息,请参阅包文档的“通过标识符获取包”部分。
What does work is +bundleWithPath:
, e.g.:
什么工作是+bundleWithPath:
,例如:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
I wrapped that inside of a dispatch_once
block and call it any time I need to access this bundle.
我将它包裹在一个dispatch_once
块中,并在需要访问此包时随时调用它。
I'm using the s.resource_bundles
syntax in my podspec as well and this works fine with it.
我s.resource_bundles
也在我的 podspec 中使用语法,这很好用。