0

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

confusedKid
  • 3,231
  • 6
  • 30
  • 49

4 Answers4

0

You can use imageview.image=[UIImage imageNamed:@"imagename"];

and by using same we can show large number of images on each imageview.It will not cause the memory issue.just need to take care of proper release for allocated imageview.

0

Is image nil?

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

tc.
  • 33,468
  • 5
  • 78
  • 96
  • 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 Aug 12 '10 at 17:51
0

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
  • 6,388
  • 2
  • 28
  • 34
  • 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 Aug 17 '10 at 16:48
  • 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 Aug 29 '10 at 19:03
0
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"happy.1" ofType:@"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation]; 
bgImageView.image = new_image;
sachi
  • 2,122
  • 7
  • 30
  • 46