Removing an Image from a UIImageView

I load a UIImageView image depending on user interaction. When the parent view is initially displayed, the image was not selected and the image was black. If the user leaves this view and returns, the image is still present. I tried

 myImageView.image = nil; 

when you exit the view, but the image remains. How to delete image so that UIImageView black again?

UIImageView

connects via IB.

+43
iphone cocoa-touch uiimage uiimageview
Jan 02 '09 at 16:36
source share
5 answers

Install UIImageView as:

 myImageView.image = nil 

is the right way to clear UIImageView . Do you load an image when your call function returns? Is your UIImageView and / or used elsewhere in your main function?

+106
Jan 02 '09 at 16:44
source share

Try specifying [self.imageView setNeedsDisplay]; after setting the image to zero. This will cause a redraw of this element in the next runloop loop that I count. Aka, you mark this opinion as "dirty" and need to be redrawn.

+4
Oct 17 '14 at 1:44 on
source share

I had a similar problem with UIImageView on my UIButton where I set the image using

 [button setImage:image forState:UIControlStateNormal]; 

By setting it to nil, you delete the image, but call getter reset the image again. I fixed it with

 [button setImage:nil forState:UIControlStateNormal]; 

instead

 [button.imageView.image = nil]; 
+3
Jun 25 2018-12-18T00:
source share

I also had the same problem, regardless of whether I use myImageView.image = nil or [myImageView setImage:nil] , the image is still displayed on a UIImageView ; assuming one should work. He spends many hours debugging and figuring out where the problems are. Finally, I did this using the following code,

  [UIView beginAnimations:nil context:nil]; [UIView animateWithDuration:1 animations:nil]; myImageView.image = nil; [UIView commitAnimations]; 

I don’t know why, but when I work as animation, then the image can be understood.

+2
Aug 17 '11 at 15:50
source share

You can use UITapGestureRecognizer to delete an image for a given number of clicks.

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTapped:)]; tapGesture.delegate = self; tapGesture.numberOfTapsRequired = 2; // no. of taps (clicks) on which you want to remove the image [myImageView addGestureRecognizer:tapGesture]; 

and give a definition of the method:

 - (void)onImageTapped:(UITapGestureRecognizer*)recognizer { UIImageView *imgView = (UIImageView*) recognizer.view; [imgView removeFromSuperview]; } 
+2
Apr 10 '13 at 9:32 on
source share



All Articles