UIImage 将原始像素从白色更改为透明?
我已经尝试了这些问题中的一些代码:
如何遮盖 UIImage 以便白色在 iphone 上变得透明?
但没有成功,不幸的是使用核心图形和图像不是我的强项。
我将如何访问 UIImage 的原始数据并将白色像素更改为清除?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅文档。
您会发现无法获取 UIImage 背后的原始数据。您可以获得的最接近的是 CGImage。这将让您获得其数据提供者,您可以向其索取原始数据的副本。
该解决方案的问题是您需要处理 CGImage 支持的所有可能的配置(RGBA、ARGB、RGB_、_RGB、RGB、8-bpc、16-bpc 等)。这是大量的工作。如果您不这样做,那么有一天,您会惊讶地发现某个图像在某种程度上无法与您的代码一起使用,或者操作系统升级改变了 CGImage 的创建方式。
针对您链接到的其他问题之一建议的 CGImageCreateWithMaskingColors 函数 是正确的解决方案。
让您困惑的一件事是 Quartz 2D 编程指南至少在 < a href="http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_color/dq_color.html#//apple_ref/doc/uid/TP30001066-CH205-SW1" rel="nofollow noreferrer">两个。地点。
我也反对包含该答案的
createMask:
方法,因为它没有按照它所说的那样做,而且几乎没有用(只有当源图像可能是 CMYK 时才值得拥有,但是如何可能是在 iPhone 应用程序上吗?)。跳过它并直接从 UIImage 的 CGImage 创建遮罩图像。一旦解决了这两个问题,这个答案可能就会很好地发挥作用。
Look at the documentation.
You'll find that there is no way to get the raw data behind a UIImage. The closest you can get is a CGImage. That will let you get its data provider, which you can ask for a copy of the raw data.
The problem with that solution is that you need to handle every possible configuration (RGBA, ARGB, RGB_, _RGB, RGB, 8-bpc, 16-bpc, etc.) that CGImage supports. That's a lot of work. If you don't do it, then someday, you'll get surprised by an image that somehow doesn't work with your code, or by an OS upgrade changing how the CGImage gets created.
The
CGImageCreateWithMaskingColors
function, suggested on one of the other questions you linked to, is the correct solution.One thing that's tripping you up is that the values shown in the accepted answer on that question are generally bogus: They're out of range. The Quartz 2D Programming Guide has more details in at least two.places.
I also argue against including that answer's
createMask:
method, since it doesn't do what it says it does and is barely useful at all (it's only worth having if the source image may be CMYK, but how likely is that on an iPhone app?). Skip it and create the mask image from the UIImage's CGImage directly.That answer will probably work just fine once you fix those two problems.