2014-12-15 3 views
0

다음 코드를 사용하여 uiimage를 저장합니다. 이미지를 저장하기 전에 이미지가 비어 있거나 비어 있는지 확인하고 싶습니다.uiimage 확인 방법은 비어 있거나 비어 있습니다.

UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

참고 :

UIImage, check if contain image How to check if a uiimage is blank? (empty, transparent)

나는 위의 링크에서 해결책을 찾을 수 없습니다. 감사!!!

답변

0

위의 문제에 대한 해결책을 찾았습니다. 이것은 미래에 누구에게나 도움이 될 수 있습니다. 이 기능은 PNG 투명 이미지까지도 완벽하게 작동합니다.

-(BOOL)isBlankImage:(UIImage *)myImage 
{ 
typedef struct 
{ 
    uint8_t red; 
    uint8_t green; 
    uint8_t blue; 
    uint8_t alpha; 
} MyPixel_T; 

CGImageRef myCGImage = [myImage CGImage]; 

//Get a bitmap context for the image 
CGContextRef bitmapContext = CGBitmapContextCreate(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; 
} 
관련 문제