How to check if uiimage is empty? (empty, transparent)

which is the best way to check if a UIImage empty?
I have a drawing editor that returns a UIImage ; I do not want to save this image if there is nothing on it.

+4
source share
5 answers
 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> 
+4
source

One idea would be to call UIImagePNGRepresentation to get an NSData object, and then compare it with a predefined "empty" version - i.e.: call:

 - (BOOL)isEqualToData:(NSData *)otherData 

check?

Have not tried this on big data; you may need to check performance if your image data is large enough, otherwise if it is small, it probably just calls memcmp() in C.

+2
source

Something like that:

  • Create a 1 px square CGContext
  • Draw an image to fill the context.
  • Test one pixel of the context to see if it contains any data. If it is completely transparent, think that the image is empty.

Others may be able to add additional information to this answer.

+2
source

I am not on my Mac, so I cannot verify this (and probably compilation errors). But one way could be:

 //The pixel format depends on what sort of image you're expecting. If it 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 blank may differ from mine if(p.red > 0 && p.green > 0 && p.blue > 0 && p.alpha > 0) return NO; } return YES; 
0
source

I ran into the same problem. Having solved it, check the dimensions:

Swift example:

 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 } 
0
source

Source: https://habr.com/ru/post/1336142/


All Articles