Cannot programmatically show image in UIImageView (iPhone)

断了今生、忘了曾经 提交于 2019-12-11 22:19:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!