IPhone - Loading local images from local xml file

Why can I download a local XML file from the main package, but I cannot download images?

NSString * path = [[NSBundle mainBundle] resourcePath];

This is loading (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

This image is never loaded (nor is the image): / Users / me / Library / Application Support / iPhone Simulator / User / Applications / 5B888456-40FF-4C72-A2ED-5D5CFA287777 / MyApp.app / background.png

0


a source to share


6 answers


You can use [UIImage imageNamed:@"background.png"]

.



This will load the background.png (which should be located in the Resources folder in Xcode).

+3


a source


In a comment, you say that you are creating an NSURL for this path. To do this for a local filesystem path, such as a file in your bundle, you need to use NSURL fileURLWithPath:

.



+3


a source


There are two possibilities here:

1) Either your image file name does not match. This means that the filename is case sensitive. Thus, perhaps we are talking about small small letters.

2) You may not have noted the target name from the image property. This means that your images are not part of the purpose of the project you are compiling. To verify this, right click on any image name inside your Xcode object -> Select Get Info -> Select Target Tabs in File Info dialog -> Check Checkbox Status next to Target Name / s. In case it is selected / checked, there is a strange problem. But if it is unselected / unchecked it means the image is not added to the package file that is created after compilation.

The third possibility here is that you may not have added the image you have in mind. Recheck the project hierarchy and see if all the resources are there or not.

+1


a source


Sort of

UIImage *testimg = [UIImage imageNamed:myfilename];

if (nil==testimg) [testimg initWithContentsOfURL:myurl];

      

I'm far from the compiler right now, so I can't check if you need to load it into an NSData object before making an image of it. You should also put initWithContents ... in try ... catch ... finally block.

+1


a source


Hi Use the following code

[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "background" ofType: @ "png"]];

0


a source


NSURL * imageURL = [NSURL URLWithString: @ "bundle: //background.png"];

0


a source







All Articles