如何检查是否空白? (空的,透明的)

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

 Try this code: BOOL isImageFlag=[self checkIfImage:image]; And checkIfImage method: - (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; } You will have to add OpenGLES framework and import this in the .m file: #import <OpenGLES/ES1/gl.h> 

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

 - (BOOL)isEqualToData:(NSData *)otherData 

去testing?

没有在大数据上尝试过; 可能要检查性能,如果你的图像数据是相当大的,否则,如果它很小,它可能就像调用C中的memcmp()

沿着这些线路的东西:

  1. 创build一个1px的方形CGContext
  2. 绘制图像,以填充上下文
  3. testing上下文的一个像素以查看它是否包含任何数据。 如果它完全透明,请将图片留空

其他人可能能够添加更多的细节到这个答案。

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

 //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; 

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

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 }