访问pod中的资源 [英] Access Resources in pod

查看:143
本文介绍了访问pod中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Cocoapods资源



Cocoapods资源包



Mokacoding资源包



< a href =https://stackoverflow.com/questions/23835052/load-image-from-cocoapods-resource-bundle> David Potter资源包



<但是,没有人指出我访问资源的方向。我尝试过以下方法:

  [UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@资产] URLForResource:@my-image @ 2xwithExtension:@png]]] 
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@Assets] URLForResource: @my-imagewithExtension:@png]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@Assets.bundle] URLForResource:@my-image withExtension:@png]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@my-imagewithExtension:@png]]]
[[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@my-image @ 2xwithExtension:@png]]]
[UIImage imageNamed:@my -asset]
[UIImage imageNamed:@my-asset@2x.png]

但它们都不起作用。



<我已经使用这些替代品设置了我的podspec,并且不能使用上述内容:

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

pod更新之后,资源显示在开发窗口/我的应用程序/资源下,但是我在我的生命中无法访问它们。没有任何捆绑,也没有任何名为资产的东西。



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

解决方案

这取决于您使用的Cocoapods的版本。使用旧版本的cocoapods,你可以使用[NSBundle mainBundle]:pathForResource:ofType:

  NSString * bundlePath = [ [NSBundle mainBundle] 
pathForResource:@MyResourceBundleofType:@bundle];
NSBundle * bundle = [NSBundle bundleWithPath:bundlePath];

如果您使用较新的Cocoapods并在podspec文件中使用spec.resource_bundles而不是spec.resources,你必须避免使用mainBundle并用NSBundle bundleForClass替换它,返回与指定类关联的NSBundle对象



示例:

  NSString * bundlePath = [[NSBundle bundleForClass:[MyFrameworkClass class]] 
pathForResource:@MyResourceBundleofType :@束];
NSBundle * bundle = [NSBundle bundleWithPath:bundlePath];


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:

Cocoapods Resources

Cocoapods Resource Bundles

Mokacoding Resource Bundles

David Potter Resource Bundles

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

But none of them work.

I've setup my podspec with these alternatives and neither work with the above:

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'.

Any help or guidance would be appreciated.

解决方案

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:

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"

Example:

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

这篇关于访问pod中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆