[英]iPhone - Loading local images from a local xml file
How come I can load a local xml file from the main bundle, but I can't load images? 为什么我可以从主捆绑包中加载本地xml文件,但无法加载图像?
NSString *path = [[NSBundle mainBundle] resourcePath]; NSString * path = [[[NSBundle mainBundle] resourcePath];
This gets loaded (i know because I can trace it) /Users/me/Library/Application Support/iPhone Simulator/User/Applications/5B888456-40FF-4C72-A2ED-5D5CFA287777/MyApp.app/test.xml 这被加载(我知道,因为我可以跟踪它)/ Users / me / Library / Application Support / iPhone Simulator / User / Applications / 5B888456-40FF-4C72-A2ED-5D5CFA287777 / MyApp.app / test.xml
This image never loads (nor does any image): /Users/me/Library/Application Support/iPhone Simulator/User/Applications/5B888456-40FF-4C72-A2ED-5D5CFA287777/MyApp.app/background.png 该图像永不加载(也不提供任何图像):/ Users / me / Library / Application Support / iPhone Simulator / User / Applications / 5B888456-40FF-4C72-A2ED-5D5CFA287777 / MyApp.app / background.png
You say in a comment that you're composing a NSURL for the path. 您在评论中说,您正在为路径组成一个NSURL。 To do this for a local file system path, such as a file in your bundle, you need to use
NSURL fileURLWithPath:
. 要对本地文件系统路径(例如捆绑包中的文件)执行此操作,需要使用
NSURL fileURLWithPath:
You can use [UIImage imageNamed:@"background.png"]
. 您可以使用
[UIImage imageNamed:@"background.png"]
。
This will load background.png (which should be located in your Resources folder in Xcode). 这将加载background.png(应位于Xcode的Resources文件夹中)。
There are two possibilities here: 这里有两种可能性:
1) Either your image file name doesn't proper. 1)您的图片文件名不正确。 That means, file name is case sensitive.
这意味着文件名区分大小写。 So might be issue of capital-small letters.
因此,可能是大写小写字母的问题。
2) You might have unchecked the target name from your image property. 2)您可能未选中图像属性中的目标名称。 That means your images are not a member of the target of your project which you are compiling.
这意味着您的图像不是要编译的项目目标的成员。 To verify this, right click on any image name inside your XCode poject --> Select Get Info --> Select Targets tab from the File Info dialog --> Verify the status of the checkbox near the target name/s.
要验证这一点,请右键单击XCode项目内的任何图像名称->选择获取信息->从“文件信息”对话框中选择“目标”选项卡->验证目标名称附近的复选框的状态。 In case, it is selected/checked then there is a strange issue.
如果它被选中/选中,则存在一个奇怪的问题。 But if it's not selected/unchecked, that means the image is not being added to the bundle file which is created after compilation.
但是,如果未选中/未选中它,则表示该图像未添加到编译后创建的捆绑文件中。
The third possibility here is you might not have added the image which you're referring. 这里的第三种可能性是您可能没有添加您要引用的图像。 Re-check your project hierarchy & see all the resources are there or not.
重新检查您的项目层次结构,并查看所有资源是否存在。
Something like 就像是
UIImage *testimg = [UIImage imageNamed:myfilename];
if (nil==testimg) [testimg initWithContentsOfURL:myurl];
I'm away from compiler right now so I can't check whether you might need to download it to an NSData object before making it an image. 我现在正离开编译器,所以无法检查在制作映像之前是否需要将其下载到NSData对象。 You should also put the initWithContents... in a try... catch... finally block.
您还应该尝试将initWithContents ...捕获...最终阻止。
Hi Use the following code 嗨,使用以下代码
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]]; [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@“ background” ofType:@“ png”]]];
NSURL * imageURL = [NSURL URLWithString:@“ bundle://background.png”];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.