Unable to modify UIImageView image

.h

IBOutlet UIImageView *photoImageView; UIImage *photo; 

.m (in viewWillAppear)

 photoImageView.image = photo; 

After the view appears, I cannot change the photoImageView using:

 photoImageView.image = [UIImage imageNamed:@"XXX.png"]; 

but the value of photo changes.

 photo = [UIImage imageNamed:@"XXX.png"]; 

How it works?

+4
source share
1 answer

Try this code.

 //YourController.h @interface YourController : UIViewController { UIImageView* _photoImageView; } @property (retain, nonatomic) IBOutlet UIImageView *photoImageView; - (IBAction)changeImageAction:(id)sender; @end //YourController.m @synthesize photoImageView = _photoImageView; - (void)viewDidLoad { [super viewDidLoad]; self.photoImageView.image = [UIImage imageNamed:@"img.png"]; } - (IBAction)changeImageAction:(id)sender { self.photoImageView.image = [UIImage imageNamed:@"img_2.png"]; } 

Some notes.

First of all, you must properly bind your photoImageView IBOutlet to Xcode. For testing purposes, I created a test button ( UIButton ) that is associated with IBAction . UIImageView and test button (I created both with IB) are childs from YourController .

Remember to import images into the project and free up memory in dealloc and in viewDidUnload .

 - (dealloc) { [_photoImageView release]; _photoImageView = nil; [super dealloc] } - (void)viewDidUnload { [super viewDidUnload]; self.photoImageView = nil; } 

Hope this helps.

Edit

The code is not ARC oriented, but with minor modifications, it can also be run using ARC.

+5
source

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


All Articles