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

问题描述

看来:

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"];

不会为 Images.xcassets 资产目录中的资产返回任何内容.我也试过:

Does not return anything for assets that are inside the Images.xcassets asset catalog. I also tried:

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images"];

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images.xcassets"];

但这些都没有奏效.

有没有人成功检索到目录中资产的路径?

Has anyone had success retrieving paths to assets in the catalog?

推荐答案

同样的问题,但我认为我们不能通过 pathForResource: 访问它,除了下面:

Same problem, but I don't think we could access it via pathForResource:, except below:

UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];

已明确记录:

资产目录中的每个集合都有一个名称.您可以使用该名称以编程方式加载集合中包含的任何单个图像.到加载图像,调用 UIImage:imageNamed: 方法,传递名称包含图像的集合.

我发现在编译的app包里,出来了一个叫"Assets.car"的文件,我觉得是我项目里的全部图片集,应该是压缩包什么的.

I found that in the package of complied app, a file named "Assets.car" came out, and I think it's the entire images sets in my project, and should have been zipped or something.

请参阅 Apple 的文档:

Refer to Apple's documentation:

Xcode 5 为资产目录提供不同的功能,具体取决于在您项目的部署目标上:

  • 对于所有项目,可以使用集合名称加载单个图像.
  • 对于部署目标为 iOS 7 的项目,Xcode 会编译您的资产目录为运行时二进制文件格式,以减少下载是时候开发您的应用了.

就是这样.

我也在寻找一种不使用 imageNamed: 的方法,我不希望运行时缓存我的图像.

I'm also looking for a way that doesn't use imageNamed:, I don't want runtime to cache my images.

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

09-02 13:47