我正在检查图像是否具有透明区域(alpha)。基于此,我必须更改 UIImage
的颜色。
我已经实现了以下方法来检查图像是否具有 alpha
。
- (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;
}
此方法运行良好,但对于某些图像会出现问题。 请参见下面的图片:
![enter image description here](https://istack.dev59.com/Gt0xP.webp)
另外两张图片
![enter image description here](https://istack.dev59.com/dgGu5.webp)
![enter image description here](https://istack.dev59.com/WAQsM.webp)
这3张图片都有相同的白色背景。第一张图片也应该有一些alpha值,而不应该是0。请指导我?我的方法是否有代码错误?