Asi Givati
Asi Givati

Reputation: 1475

Detect if UIImage not contain an image

I'm trying to detect if some UIImage is empty OR contains image. All the UIImages have an instance so I can't ask if image == nil. I tried also this code:

CGImageRef cgref = [image CGImage];
CIImage *cim = [image CIImage];

if (cim == nil && cgref == NULL)
{
    NSLog(@"no underlying data");
}

But its not working for me. I also tried to convert the image to NSData and check the size of the bytes but the "empty" image size is close to the good one.

BTW - the size of all the UIImages are the same (width & height).

Any other ideas?

enter image description here

VS

enter image description here

Upvotes: 1

Views: 782

Answers (2)

yariksmirnov
yariksmirnov

Reputation: 489

Try to use CGImageGetAlphaInfo() and check if value is kCGImageAlphaOnly

Actually, its just an idea. I'm not able check by myself at the moment.

Upvotes: 0

poyo fever.
poyo fever.

Reputation: 752

- (NSString *)contentTypeForImageData:(NSData *)data
{
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
    }
    return nil;
}

and convert you image to nsdata and test :

NSData *imageData = //convert your image to NSData
if([self contentTypeForImageData:imageData] == nil) {
     NSLog(@"no underlying imageView");
} else {
     NSLog(@"underlying imageView");
}

I hope it will help you.

Upvotes: 1

Related Questions