1

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

Asi Givati
  • 1,348
  • 1
  • 15
  • 31
  • 1
    did you checked image size and height if both are 0 and 0 than there is no image – Smile Dec 23 '15 at 07:56
  • [check this](http://stackoverflow.com/questions/5249664/iphone-how-can-i-get-the-height-and-width-of-uiimage) – Smile Dec 23 '15 at 07:57
  • Yes I tried. the width and height of all the images are the same. – Asi Givati Dec 23 '15 at 08:00
  • check using [Image isKindOfClass:[UIImage class]] – Rohit Khandelwal Dec 23 '15 at 08:03
  • It always UIImage as you can see in the added picture. – Asi Givati Dec 23 '15 at 08:28
  • Why do even the empty images have a UIImage on the left hand side of your picture. Where are you loading this information from? At the point of loading the grid on the left you could keep an array of which ones are empty? If the empty image is always that grey colour you could spot check some random pixels and if they all grey "assume" its empty - not nice though – Flexicoder Dec 23 '15 at 08:52

2 Answers2

1
- (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.

poyo fever.
  • 742
  • 1
  • 5
  • 22
  • It looks good but its not working for me :) all the results of your code are the same (with and without image). – Asi Givati Dec 23 '15 at 09:03
0

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.

yariksmirnov
  • 489
  • 5
  • 10