Removing image from UIImageView

IphoneCocoa TouchUiimageviewUiimage

Iphone Problem Overview


I load a UIImageView with an image depending on user interaction. When the parent view is initially displayed, no image has been selected and the imageview is black. If the user leaves that view and comes back, the image is still there. I've tried

myImageView.image = nil;

upon leaving the view but the image remains. How can I remove the image so the UIImageView appears black again?

The

> UIImageView

is connected through IB.

Iphone Solutions


Solution 1 - Iphone

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 - Iphone

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 - Iphone

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 - Iphone

I also got the same problem, no matter I use myImageView.image = nil or [myImageView setImage:nil] the image still show on the UIImageView; assuming either one should works. It spend me many hours to debug and find out where have problem. Finally, I did it by 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 an animation then the image can be clear perfectly.

Solution 5 - Iphone

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];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question4thSpaceView Question on Stackoverflow
Solution 1 - IphoneTammen BruccoleriView Answer on Stackoverflow
Solution 2 - IphonetheprojectabotView Answer on Stackoverflow
Solution 3 - IphoneMaxGabrielView Answer on Stackoverflow
Solution 4 - IphoneTonyNetView Answer on Stackoverflow
Solution 5 - IphoneMohd RahibView Answer on Stackoverflow