UIImage imageNamed:在蓝色引用的 Xcode 文件夹中找不到图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11499925/
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
UIImage imageNamed: does not find image within blue referenced Xcode-folder
提问by Besi
In my Xcode project I have a blue referenced folder which points to a folder with images.
在我的 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
蓝色文件夹的好处是我不必手动将文件添加到 Xcode。因此,当我将新图像添加到文件夹时,它会自动显示在 Xcode 中,但是,当我尝试使用以下代码从代码中引用文件时
[UIImage imageNamed:@"myFileFromTheBlueFolder"];
it does not find the image although the top-level folder is added to the target. :-(
尽管顶级文件夹已添加到目标,但它找不到图像。:-(
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.
当我来自一个蓝色文件夹时,我是否必须以不同的方式引用图像,恕我直言,这完全违背了拥有这样一个文件夹的目的。
采纳答案by Lily Ballard
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.
我想问题是“蓝色文件夹”(例如文件夹引用,而不是组)被逐字复制到您的应用程序资源中。换句话说,它不像资源通常那样被扁平化。如果您检查构建的产品,在所有常规资源旁边,您可能会看到文件夹本身,而文件夹中的所有内容仍在文件夹中。
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).
因此,+[UIImage imageNamed:]
无法找到图像,因为它不会递归到子目录中。您可以使用-[NSBundle pathForResource:ofType:inDirectory:]
获取资源的路径并将其传递给+[UIImage imageWithContentsOfFile:]
,但这会丢弃具有的内存优化+imageNamed:
(例如,它缓存使用过的图像并丢弃未使用的图像)。
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.
或者,您可以停止使用文件夹引用。或者,您可以从资源构建阶段删除文件夹引用,并添加一个 shell 脚本构建阶段,将其内容复制到您的构建产品中。
回答by Mr. T
Kevin is right about the memory optimizations that +[UIImage imageNamed:]
provides, but it is absolutely possible to access the images in the blue folders (folder references). All you have to do is to add the folder name into the image name. For example:
Kevin 关于提供的内存优化是正确的+[UIImage imageNamed:]
,但绝对可以访问蓝色文件夹(文件夹引用)中的图像。您所要做的就是将文件夹名称添加到图像名称中。例如:
Your resources are organized like this:
您的资源是这样组织的:
Icons (blue folder / folder reference)
|__camera.png
|__checkmark.png
Shared (yellow folder / xcode group)
|__back.png
|__logo.png
You can access the images like this:
您可以像这样访问图像:
[UIImage imageNamed:@"Icons/camera"];
[UIImage imageNamed:@"Icons/checkmark"];
[UIImage imageNamed:@"back"];
[UIImage imageNamed:@"logo"];
回答by Sanchit Paurush
Write complete name of the image.
写下图像的完整名称。
[UIImage imageNamed:@"myFileFromTheBlueFolder.png"];
Also keep in mind for case Insensitive names.
还要记住不区分大小写的名称。