Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for an empty UIImageView?

Tags:

I have the following code where I want to see if a particular UIImageView (image) is set. If not then I want to display an error message.

if (image==nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"No Image Selected" 
                                                                 delegate:nil 
                                                        cancelButtonTitle:@"OK" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:nil];

        [actionSheet showInView:[[self view]window]];
        [actionSheet autorelease];
    }
like image 477
jini Avatar asked Jun 13 '11 01:06

jini


People also ask

How check UIImage is nil or not in Swift?

let image: UIImage? = UIImage(contentsOfFile: filePath)if image != nil { return image!}

What is UIImage?

An object that manages image data in your app.


2 Answers

imageView.image == nil ? Or, to check for empty images, CGSizeEqualToSize(imageView.image.size, CGSizeZero).

like image 195
Wilbur Vandrsmith Avatar answered Sep 18 '22 22:09

Wilbur Vandrsmith


It may be tricky to figure out if (UIImageView *)image has been initialized with an image. You may want to check the image property:

if (image.image == nil)

This will work in the case where image.image==nil (UIImageView initialized by image not set) AND in the case where image==nil (since messages sent to nil return nil).

Beware local variables as well: Just because you never alloc/init image doesn't mean it will be nil.

like image 7
MikeJfromVA Avatar answered Sep 19 '22 22:09

MikeJfromVA