UIImage imageNamed需要pathForResource吗? [英] UIImage imageNamed requires pathForResource?

查看:193
本文介绍了UIImage imageNamed需要pathForResource吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建<时,使用 NSBundle 方法 pathForResource 搜索图像路径的必要性如何? code> UIImage 使用 imageNamed ?我看到教程代码只是直接指定图像的名称,然后代码更加努力地找到路径。

How necessary is it to search for a path to an image using the NSBundle method pathForResource when creating a UIImage using imageNamed? I see tutorial codes that just specifies the name of the image directly, and then code that goes the extra mile to find the path first.

根据我的经验,我已经总是只是直接使用名称,它总是很好。我以为它会自动知道如何找到图像。有多重要或在什么情况下需要做更多的事情?

In my experience, I've always just used the name directly and it's always worked fine. I assumed that it automatically knew how to find the image. How important or under what circumstances would it be necessary to do more than this?

推荐答案

文档说该方法寻找应用程序主包中具有指定名称的图像,所以我说你总是可以只使用名称。唯一的例外可能是存储在子文件夹中的图像,尤其是当你有 foo / image.png bar / image.png 。我不知道 [UIImage imageNamed:@foo / image] 是否可行,但尝试这是微不足道的。

The docs say that "the method looks for an image with the specified name in the application’s main bundle", so I’d say you can always use just the name. The only exception might be images stored inside subfolders, especially when you have foo/image.png and bar/image.png. I don’t know whether [UIImage imageNamed:@"foo/image"] would work, but it’s trivial to try.

(在这些情况下有点令人困惑的是,Xcode树中的组与生成的应用程序包中的文件夹不对应。它们的内容被一起粉碎到包的根目录,除非您使用蓝色文件夹引用而不是常规组。)

(What’s a bit confusing in these cases is that the groups in the Xcode tree do not correspond to folders in the resulting app bundle. Their contents are smashed together to the root of the bundle, unless you use a blue folder reference instead of a regular group.)

这篇关于UIImage imageNamed需要pathForResource吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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