简体   繁体   English

检测UIImage是否不包含图像

[英]Detect if UIImage not contain an image

I'm trying to detect if some UIImage is empty OR contains image. 我正在尝试检测某些UIImage是否为空或包含图像。 All the UIImages have an instance so I can't ask if image == nil . 所有的UIImage都有一个实例,所以我不能问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. 我还尝试将图像转换为NSData并检查字节的大小,但“空”图像大小已接近良好的大小。

BTW - the size of all the UIImages are the same (width & height). 顺便说一句-所有UIImage的大小都相同(宽度和高度)。

Any other ideas? 还有其他想法吗?

在此处输入图片说明

VS 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并测试:

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 尝试使用CGImageGetAlphaInfo()并检查值是否为kCGImageAlphaOnly

Actually, its just an idea. 实际上,这只是一个想法。 I'm not able check by myself at the moment. 我目前无法自己检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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