问题
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
回答1:
Is image nil?
What's wrong with [UIImage imageNamed:@"happy.1.jpg"]?
回答2:
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.
回答3:
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"happy.1" ofType:@"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
bgImageView.image = new_image;
回答4:
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.
来源:https://stackoverflow.com/questions/3470354/cannot-programmatically-show-image-in-uiimageview-iphone