如何检查一个UIImage是否为空白?(即空的,透明的)

8

最佳方法是检查一个UIImage是否为空白?
我有一个绘画编辑器,它会返回一个UIImage;如果上面没有任何东西,我不想保存这个图片。

6个回答

5

试试这段代码:

BOOL isImageFlag=[self checkIfImage:image];

并且检查 checkIfImage 方法:

- (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 = NO;
    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 = YES;
            break;
        }
    }

    free(imageData);        
    return imageExist;
}

您需要添加OpenGLES框架并在.m文件中导入它:

#import <OpenGLES/ES1/gl.h>

2

一个想法是调用UIImagePNGRepresentation函数获取一个NSData对象,然后将其与预定义的“空”版本进行比较 - 即调用:

- (BOOL)isEqualToData:(NSData *)otherData

要测试吗?

如果你的图像数据很大,可能需要检查性能,但如果数据比较小,使用类似于C语言中的memcmp()


这可能不是一个好主意,因为苹果随时可能微调他们生成的PNG文件。 - Mike Abdullah

2

以下是相关内容的翻译:

  1. 创建一个1像素的正方形CGContext
  2. 绘制图像,使其填充上下文
  3. 测试上下文的一个像素,以查看它是否包含任何数据。如果完全透明,则认为图片为空白

其他人可能会在此答案中添加更多详细信息。


1

这里提供了一种Swift的解决方案,不需要任何额外的框架。

感谢在这里相关问题中的答案: 如何在xcode上通过触摸屏幕的坐标获取ImageView的像素数据?

func imageIsEmpty(_ image: UIImage) -> Bool {
    guard let cgImage = image.cgImage,
          let dataProvider = cgImage.dataProvider else
    {
        return true
    }

    let pixelData = dataProvider.data
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
    let imageWidth = Int(image.size.width)
    let imageHeight = Int(image.size.height)
    for x in 0..<imageWidth {
        for y in 0..<imageHeight {
            let pixelIndex = ((imageWidth * y) + x) * 4
            let r = data[pixelIndex]
            let g = data[pixelIndex + 1]
            let b = data[pixelIndex + 2]
            let a = data[pixelIndex + 3]
            if a != 0 {
                if r != 0 || g != 0 || b != 0 {
                    return false
                }
            }
        }
    }

    return true
}

0

我不在我的Mac上,所以我无法测试这个(可能会有编译错误)。但是可能有一种方法:

//The pixel format depends on what sort of image you're expecting. If it's RGBA, this should work
typedef struct
{
   uint8_t red;
   uint8_t green;
   uint8_t blue;
   uint8_t alpha;
} MyPixel_T;

UIImage *myImage = [self doTheThingToGetTheImage];
CGImageRef myCGImage = [myImage CGImage];

//Get a bitmap context for the image
CGBitmapContextRef *bitmapContext = 
CGBitmapContextFreate(NULL, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage), 
                      CGImageGetBitsPerComponent(myCGImage), CGImageGetBytesPerRow(myCGImage),
                      CGImageGetColorSpace(myCGImage), CGImageGetBitmapInfo(myCGImage));

//Draw the image into the context
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage)), myCGImage);

//Get pixel data for the image
MyPixel_T *pixels = CGBitmapContextGetData(bitmapContext);
size_t pixelCount = CGImageGetWidth(myCGImage) * CGImageGetHeight(myCGImage);

for(size_t i = 0; i < pixelCount; i++)
{
   MyPixel_T p = pixels[i];
   //Your definition of what's blank may differ from mine
   if(p.red > 0 && p.green > 0 && p.blue > 0 && p.alpha > 0)
      return NO;
}

return YES;

-2

我刚遇到了同样的问题。通过检查尺寸解决了它:

Swift 示例:

let image = UIImage()

let height = image.size.height
let width = image.size.height

if (height > 0 && width > 0) {
    // We have an image
} else {
    // ...and we don't
}

1
这不是一个解决方案。一个没有内容的PNG文件(alpha通道处于0)仍然可以返回高度和宽度。 - Allison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接