How to change the contents of a UIImageView image by code?

I use this to assign an 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 this:

  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 the image on top of the top. How can I change my code? thanks u.

+4
source share
4 answers

Alternatively, this can be done using tags.

 //Set up the imageView in viewDidLoad or similar UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)]; [imageView setTag:123]; [self.view addSubview:imageView]; [imageView release]; 

Then anytime you want to change the image, you just need to use:

 [(UIImageView*)[self.view viewWithTag:123] setImage:[UIImage imageWithCGImage:imageRef]]; 

Another option, the rest of the answers will work just as well.

+6
source

I just spent the whole day on this difficult problem, so I thought I would share how to change images in UIImageView. Too many posts regarding this could not show an easy way to do this, here it is ...

If you want to change the image displayed on the image View

In your .h

 IBOutlet UIImageView *imageView; @property (nonatomic, retain, readwrite) IBOutlet UIImageView *imgView; 

and in your .m

 -(void)viewDidLoad { [super viewDidLoad]; self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(133, 230, 60, 60)]; self.imageView.image = [UIImage imageNamed:@"firstPicure.png"]; [self.view addSubview:self.imageView]; } 

Now change the image displayed in your image. One of the nice things about this method is that imageNamed will return an image autoresist instance to avoid memory leaks and for simplicity you use performSelectorOnMainThread to handle the change

 -(void)changeImage { [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:@"secondPicture.png"] waitUntilDone:YES]; } 
+4
source

I'm not sure why you are trying to crop the image; just set the contentMode property accordingly. Alternative approach:

 UIImage *image = [UIImage imageNamed:@"myImage.png"]; UIImage *image2 = [UIImage imageNamed:@"myImage2.png"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:...]; imageView.contentMode = UIViewContentModeTopLeft; imageView.image = image; [self.view addSubView:imageView]; ... imageView.image = image2; 
+3
source

You are currently creating a new UIImageView and adding it to the list of subviews. As you can see, in the end you get two images. You just want to change the image displayed in the first view. Keep the handle of the UIImageView you created (possibly as member fields), and when you want to change the image, just write

 UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage2" ofType:@"png"]]; imageView.image = [UIImage imageWithCGImage:imageRef]; 
+2
source

Source: https://habr.com/ru/post/1308658/


All Articles