UIImage imageNamed:在蓝色引用的Xcode文件夹中找不到图像 [英] UIImage imageNamed: does not find image within blue referenced Xcode-folder

查看:651
本文介绍了UIImage imageNamed:在蓝色引用的Xcode文件夹中找不到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Xcode项目中,我有一个蓝色的引用文件夹指向一个包含图像的文件夹。

In my Xcode project I have a blue referenced folder which points to a folder with images.

蓝色文件夹的好处是我不必手动将文件添加到Xcode。
所以当我添加一个新的图像到文件夹时,它会自动显示在Xcode中,但是,当我尝试使用

The benefit of the blue folder is that I don't have to add the files to Xcode manually. So when I add a new image to the folder it automatically shows up in Xcode, however, when I try to reference the file from code using

[UIImage imageNamed:@"myFileFromTheBlueFolder"];

它找不到图像,尽管顶层文件夹已添加到目标。 : - (

it does not find the image although the top-level folder is added to the target. :-(

当我来自一个蓝色文件夹时,我必须以不同的方式引用图片,这将使IMHO完全失去这样一个文件夹的目的。 / p>

Do I have to reference the image differently when I is coming from a blue folder, which would IMHO defeat the purpose of having such a folder at all.

推荐答案

我想这个问题是蓝色文件夹(例如文件夹引用,而不是一个组)如果你检查你的产品,在你所有的正常资源旁边你可能会看到文件夹本身,并且文件夹中的一切仍然在文件夹中。

I imagine the problem is the "blue folder" (e.g. folder reference, instead of a group) is being copied into your App resources verbatim. In other words, it's not being flattened like resources typically are. If you check your built product, next to all your normal resources you'll probably see the folder itself, and everything that was in the folder is still in the folder.

因此, + [UIImage imageNamed:] 找不到图片,因为它不递归您可以使用 - [NSBundle pathForResource:ofType:inDirectory:] 获取资源的路径并将其传递给 + [UIImage imageWithContentsOfFile :] ,但这放弃了 + imageNamed:具有的内存优化(例如,它缓存使用的图像并丢弃未使用的图像)。

As such, +[UIImage imageNamed:] can't find the image because it doesn't recurse into subdirectories. You can use -[NSBundle pathForResource:ofType:inDirectory:] to get a path to the resource and pass that to +[UIImage imageWithContentsOfFile:], but this discards the memory optimizations that +imageNamed: has (e.g. where it caches used images and drops unused images).

或者,您可以停止使用文件夹引用。或者,您可以从资源构建阶段删除文件夹引用,并添加一个shell脚本构建阶段,将其内容复制到您的构建产品中。

Alternatively, you could stop using a folder reference. Or you could remove the folder reference from your resources build phase and add a shell script build phase that copies its contents into your built product instead.

这篇关于UIImage imageNamed:在蓝色引用的Xcode文件夹中找不到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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