Problem checking UIImage has alpha (transparent) color or not

hi i m check that the image has a transparent area (alpha) or not. Due to the fact that I need to change the color of the UIImage

I use the method below to check the alpha image or not.

 - (BOOL) checkAlpha : (UIImage*) image { for(int x = 0; x < image.size.width ; x++) { for(int y = 0; y < image.size. height; y++) { CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); const UInt8* data = CFDataGetBytePtr(pixelData); int pixelInfo = ((image.size.width * y) + x ) * 4; // The image is png UInt8 red = data[pixelInfo]; // If you need this info, enable it UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it UInt8 blue = data[pixelInfo + 2]; // If you need this info, enable it UInt8 alpha = data[pixelInfo + 3]; // I need only this info for my maze game CFRelease(pixelData); UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; DLog(@"color : %@",color); DLog(@"alpha : %hhu",alpha) if (alpha) return YES; // display original image from url. else return NO; // apply brand color here. } } return YES; } 

This method works fine, but for some image it creates a problem. See image below.

enter image description here For this image, alpha returns 0;

enter image description here enter image description here And for the above 2 images, alpha has some meaning.

All 3 images have the same white background. There must also be alpha for the first image. it should not be 0. Please help me find out about this? is there any code error in my method or what?

+4
ios objective-c uiimage alpha-transparency
Aug 17 '16 at 14:21
source share
4 answers

Finally, find a solution for the current images displayed in the question. I want to know if UIImage has an alpha channel or not.

 - (BOOL) hasAlphaChannel : (UIImage*) image { if ([self hasAlpha:image]) return YES; return NO; } - (BOOL)hasAlpha : (UIImage*) img { CGImageAlphaInfo alpha = CGImageGetAlphaInfo(img.CGImage); return ( alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast || alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast ); } 

This hasAlpha method works fine in this case.

For more information: Check out this link.

+2
Sep 01 '16 at 13:42 on
source share

For what it's worth, I tried uploading your images by right-clicking and selecting "get info." The information says that the images do not have an alpha channel, so I would start by getting new versions of these images that have alpha.

+1
Aug 23 '16 at 17:24
source share

Run all the points on the image \ layer and check for the existence of an alpha channel like this:

  UIColor *pixelColor = [SSAlphaPass colorOfPoint:point withLayer:self.layer]; if(CGColorGetAlpha(pixelColor.CGColor)) { return YES; } return NO; 



 + (UIColor *) colorOfPoint:(CGPoint)point withLayer:(CALayer*)layer { unsigned char pixel[4] = {0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(context, -point.x, -point.y); [layer renderInContext:context]; CGContextRelease(context); CGColorSpaceRelease(colorSpace); //NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]); UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0]; return color; } 

I wrote a class that determines if a particular pixel is transparent if that UIButton case, for example, passes the Touch event to its UIButton . You may find it helpful .

+1
Aug 24 '16 at 10:50
source share

Firstly, your call to CGDataProviderCopyData () is inside the loop, so it will be terribly slow. Secondly, your code will not work with various types of PNG images. The only way to make it work properly is to map the input image to a pixel buffer, and then check the result, once smoothed into simple pixels.

-one
Aug 17 '16 at 16:45
source share



All Articles