How to change the UIImageView image content by code?
Posted
by Tattat
on Stack Overflow
See other posts from Stack Overflow
or by Tattat
Published on 2010-05-03T13:04:05Z
Indexed on
2010/05/03
13:08 UTC
Read the original article
Hit count: 256
I use this to assign image:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"]];
CGRect cropRect = CGRectMake(175, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
imageView.image = [UIImage imageWithCGImage:imageRef];
[self addSubview:imageView];
CGImageRelease(imageRef);
Now, I want to change the image from myImage to myImage2, so I do that:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage2" ofType:@"png"]];
CGRect cropRect = CGRectMake(175, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
imageView.image = [UIImage imageWithCGImage:imageRef];
[self addSubview:imageView];
It works, but not the way I want, I want to change the image, not add a imageView on top of the pervious one. How can I modify my code? thx u.
© Stack Overflow or respective owner