2011-01-19 77 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