简体   繁体   中英

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?

在此处输入图片说明

VS

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM