开发者

what is the proper way to set a UIImageView with a UIImage?

开发者 https://www.devze.com 2022-12-24 17:20 出处:网络
I have a UIImageView that starts off with an image (loading.png) specified in IB. When my app starts, a new image is downloaded from the internet to replace the existing one. I do the following to rep

I have a UIImageView that starts off with an image (loading.png) specified in IB. When my app starts, a new image is downloaded from the internet to replace the existing one. I do the following to replace the image:

if(object.imageView.image != nil){

[object.imageView.image release];
object.imageView.image = nil开发者_C百科;               
}       
object.imageView.image = [UIImage imageNamed:@"downloadedimage.png"];

Is this the right way to re-set an image? I am getting EXC_BAD_ACCESS when doing so.


UIImageView's image property is declared as

@property(nonatomic, retain) UIImage *image;

That means when you set this property, the old value will be automatically -release'd. So this 1 line is enough:

object.imageView.image = [UIImage imageNamed:@"downloadedimage.png"];
0

精彩评论

暂无评论...
验证码 换一张
取 消