UIImage 将原始像素从白色更改为透明?

发布于 2024-08-28 17:32:44 字数 402 浏览 2 评论 0 原文

我已经尝试了这些问题中的一些代码:

如何使 UIImage 上的一种颜色透明?

如何遮盖 UIImage 以便白色在 iphone 上变得透明?

但没有成功,不幸的是使用核心图形和图像不是我的强项。

我将如何访问 UIImage 的原始数据并将白色像素更改为清除?

I've tried some code from each of these questions:

How to make one color transparent on a UIImage?

How to mask a UIImage so that white becomes transparent on iphone?

but have come up unsuccessful, unfortunately working with Core Graphics and images is not my strong suit.

How would I go about accessing a UIImage's raw data and changing the white pixels to clear?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-09-04 17:32:44

我将如何访问 UIImage 的原始数据......?

请参阅文档

您会发现无法获取 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 创建遮罩图像。

一旦解决了这两个问题,这个答案可能就会很好地发挥作用。

How would I go about accessing a UIImage's raw data …?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文