ios - 检查图像是否全白的最佳性能方法?

标签 ios objective-c performance core-graphics

我正在尝试确定当前绘图是否全是白色。我想出的解决方案是缩小图像,然后逐个像素地检查它是否为白色,并在发现不是白色的像素时立即返回 NO。

它有效,但我有一种直觉,它可以以更高效的方式完成。这是代码:

- (BOOL)imageIsAllWhite:(UIImage *)image {
    CGSize size = CGSizeMake(100.0f, 100.0f);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[image scaledImageWithSize:size]];


    unsigned char pixel[4 * (int)size.width * (int)size.height];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef cgContext = CGBitmapContextCreate(
            pixel,
            (size_t)size.width,
            (size_t)size.height,
            8,
            (size_t)(size.width * 4),
            colorSpace,
            kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(cgContext, 0, 0);

    [imageView.layer renderInContext:cgContext];

    CGContextRelease(cgContext);

    CGColorSpaceRelease(colorSpace);

    for (int i = 0; i < sizeof(pixel); i = i + 4) {
        if(!(pixel[i] == 255 && pixel[i+1] == 255 && pixel[i+2] == 255)) {
            return NO;
        }
    }

    return YES;
}

有什么改进的想法吗?

最佳答案

请按照以下代码检查 UIImage 是否为白色

- (BOOL) checkIfImage:(UIImage *)someImage {
    CGImageRef image = someImage.CGImage;
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    GLubyte * imageData = malloc(width * height * 4);
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * width;
    int bitsPerComponent = 8;
    CGContextRef imageContext =
    CGBitmapContextCreate(
                          imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
                          );

    CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
    CGContextRelease(imageContext);

    int byteIndex = 0;

    BOOL imageExist = YES;
    for ( ; byteIndex < width*height*4; byteIndex += 4) {
        CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
        CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
        CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
        CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
        if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
            imageExist = NO;
            break;
        }
    }

    return imageExist;
}

调用函数

 UIImage *image = [UIImage imageNamed:@"demo1.png"];
    BOOL isImageFlag=[self checkIfImage:image];
    if (isImageFlag == YES) {
            NSLog(@"YES it's totally White");
    }else{
            NSLog(@"Nope it's not White");
    }

关于ios - 检查图像是否全白的最佳性能方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732475/

相关文章:

ios - 异步加载数据到 UITableView

ios - 静态变量如何在实例方法中工作 - 单例

ios - 使用 +1000 值转换日期

ios - 不通过 iTunes 在多个 iPhone 设备上测试我的 Titanium 应用程序?

ios - 如何在 iPhone 应用程序的 url 中传递一个字符串值?

android - 创建新 Activity 最有效的方法是什么

iphone - JSON 对象到 NSArray

iphone - AFNetworking 的多个请求

c - 对整个 4Kb block 使用 mmap() 是否可以,还是一次性对我的整个文件使用 mmap() 更好?

Android 应用程序内存不足错误,尝试使用 88kb 图像