Removing image from UIImageView

Solution 1:

Setting the UIImageView as:

myImageView.image = nil 

is the correct way to clear an UIImageView. Are you loading the image when your calling function returns? Is your UIImageView being declared, and/or used elsewhere in your main function?

Solution 2:

Try stating [self.imageView setNeedsDisplay]; after your image setting to nil. This will trigger a redraw of that element on the next runloop cycle I believe. Aka you are marking this view as 'dirty' and needs a redraw.

Solution 3:

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

[button setImage:image forState:UIControlStateNormal];

Setting it to nil would remove the image, but calling the getter again reset the image. I fixed it with:

[button setImage:nil forState:UIControlStateNormal];

instead of

[button.imageView.image = nil];

Solution 4:

You can use a UITapGestureRecognizer to remove the 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 the method's definition:

- (void)onImageTapped:(UITapGestureRecognizer*)recognizer
{

    UIImageView *imgView = (UIImageView*) recognizer.view;
    [imgView removeFromSuperview];
}