views:

168

answers:

2

Hi, I'm developing a iPhone app, but I ran into a snag.

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"happy.1" ofType:@"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation]; 
[self setImage:new_image];

if (new_image == nil) {
    NSLog(@"can't load image happy.1.jpg");
}
bgImageView.image = image;

That's the code I'm using to show an image, but it doesn't appear! new_image isn't nil, which I understand it would be if it couldn't load the image. bgImageView is an IBOutlet and connected in IB.

Please tell me what I'm doing wrong. Thanks!

Edit: also, this does not work on the simulator nor device

A: 

Is image nil?

What's wrong with [UIImage imageNamed:@"happy.1.jpg"]?

tc.
I will have to load many images later on, so I thought that using imageWithContentsofFile will save me memory in the long run. image is not nil either.
confusedKid
A: 

So, usually this is because the images are not getting copied into the built executable. So, open the application package and see if they are there.

If then maybe check that fileLocation doesn't come back empty/nil.

I wonder if the ".1" is causing a problem. Try renaming you image file _1, _2 and see if that fixes it.

Dad
I printed the fileLocation, and it's "/var/mobile/Applications/455212CA-C53C-449F-9125-5C4B0627070F/Mootchi.app/happy_1.jpg" so it looks ok. I also renamed to happy_1, but still nothing.
confusedKid
You need to use the debugger and step through this code. Check that bgImageView is not nil, look at it's dimensions, make sure it's actually visible. Is the view actually in a visible window? The general rule for tracking down things like this when you've done everything else is: simplify it. One window, one view, one image. Get that working, then make it more complicated.
Dad