0
votes

How come I can load a local xml file from the main bundle, but I can't load images?

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

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

6
What do you mean when you say the image never loads? How are you attempting to load it?Ryan
-1. Not enough code, no code showing expected vs actual behaviour (e.g. with asserts?)...tc.

6 Answers

3
votes

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

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

3
votes

You say in a comment that you're composing a NSURL for the path. To do this for a local file system path, such as a file in your bundle, you need to use NSURL fileURLWithPath:.

1
votes

There are two possibilities here:

1) Either your image file name doesn't proper. 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. 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. 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.

1
votes

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. You should also put the initWithContents... in a try... catch... finally block.

0
votes

Hi Use the following code

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

0
votes

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