UIImage将原始像素从白色更改为清除? [英] UIImage change raw pixels from white to clear?

查看:166
本文介绍了UIImage将原始像素从白色更改为清除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过以下每个问题的代码:

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

How to make one color transparent on a UIImage?

如何掩饰UIImage,使白色在iPhone上变得透明?

但是已经不成功,不幸的是使用Core Graphics和图片不是我的强项。

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

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

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

推荐答案


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

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

查看文档

发现没有办法获得UIImage背后的原始数据。您可以获得的最接近的是 CGImage 。这将让你获得它的数据提供程序,你可以要求一个原始数据的副本。

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.

该解决方案的问题是,你需要处理每一个可能的配置(RGBA,ARGB,RGB_,_RGB,RGB,8-bpc,16-bpc等)。这是很多的工作。如果你不这样做,有一天,你会感到惊讶的图像,不知何故不能与你的代码,或者一个操作系统升级改变如何创建的CGImage。

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.

CGImageCreateWithMaskingColors 函数,建议您链接到的其他问题之一,是正确的解决方案。

The CGImageCreateWithMaskingColors function, suggested on one of the other questions you linked to, is the correct solution.

让你失望的一件事是显示在对该问题的接受答案通常是假的:他们超出范围。 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 =nofollow noreferrer>两个。地方

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.

我也反对包括该答案 createMask: / code>方法,因为它不会做它说的它,并且几乎没有什么有用的(只有值得拥有,如果源图像可能是CMYK,但在iPhone应用程序有多大的可能性?)。跳过它,直接从UIImage的CGImage创建掩码图像。

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.

这篇关于UIImage将原始像素从白色更改为清除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆