2011-01-19 56 views

回答

2

一个想法是调用UIImagePNGRepresentation得到一个NSData对象,然后将其与预先定义的“空”版本进行比较 - 即:拨打:

- (BOOL)isEqualToData:(NSData *)otherData 

测试?

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

+0

也许不是一个好主意,因为苹果可能会微妙地改变它们产生的任何一点PNG文件。 – 2011-01-19 14:45:48

2

东西沿着这些路线:

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

其他人也许能够在此答案中添加更多详细信息。

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; 
4
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> 
+0

这对我有用!非常感谢,这么好的解决方案 – WildMassacre 2015-06-29 19:15:24

0

我正好遇到了同样的问题。通过检查维度解决它:

斯威夫特例如:

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)仍然可以返回高度和宽度 – Sirens 2016-04-24 04:59:35