我的 iOS 应用程序上有一些部分透明的图像(格式为 PNG)。
我可以找到图像上非透明区域的 CGRect 区域吗?
最佳答案
我不知道有什么功能可以开箱即用。 但是你可以编写自己的函数。您需要做的就是一个一个地获取像素的颜色,并确定它们是否构成矩形。
要获得它,您可以使用以下代码。
CGImageRef image = [myUIImage CGImage];
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height));
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
red = rawData[byteIndex];
green = rawData[byteIndex + 1];
blue = rawData[byteIndex + 2];
alpha = rawData[byteIndex + 3];
这最初发布于此 question.
关于ios - 在 iOS 上的图像中查找非透明填充的 CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29468515/