steveY
steveY

Reputation:

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?

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

Upvotes: 0

Views: 2714

Answers (6)

TomSwift
TomSwift

Reputation: 39502

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:.

Upvotes: 3

theChrisKent
theChrisKent

Reputation: 15099

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

Upvotes: 0

Aditya
Aditya

Reputation: 4396

Hi Use the following code

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

Upvotes: 0

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3830

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.

Upvotes: 1

Sagar
Sagar

Reputation: 3149

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.

Upvotes: 1

pgb
pgb

Reputation: 25011

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

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

Upvotes: 3

Related Questions