ios - 在 iOS 上的图像中查找非透明填充的 CGRect

标签 ios objective-c swift

我的 iOS 应用程序上有一些部分透明的图像(格式为 PNG)。

我可以找到图像上非透明区域的 CGRect 区域吗?

App Preview

最佳答案

我不知道有什么功能可以开箱即用。 但是你可以编写自己的函数。您需要做的就是一个一个地获取像素的颜色,并确定它们是否构成矩形。

要获得它,您可以使用以下代码。

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/

相关文章:

iOS:重复动画 block 直到事件完成

ios - 查看不添加到 UITableViewCell

html - WKWebview加载本地html文件并深层链接进入目标页面

ios - 异步函数不更新变量

ios - 如何更快地将 View 渲染成图像?

ios - Appcelerator iOS 推送通知在应用程序处于前台时不起作用

ios - 使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView

swift - 将应用推送到 AppStore 时应用商店连接操作错误

ios - 使用 NSUserActivity 和 CoreSpotlight 但仍将 iOS8 设置为部署目标

iphone - UIButton 内存管理 - 如何管理它?