UIImage颜色改变?
如何通过编程改变UIImage的颜色,请帮忙?如果我发送 UIImage,其颜色需要更改,有什么帮助吗?如果我通过位图处理更改 RGB 颜色,则不起作用。
How can I change the UIImage's color through programming, any help please? If I send a UIImage, its color needs to change any help please? If I change the RGB color through bitmaphandling, it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您只需要它看起来不同,只需使用
imageView.tintColor
(iOS 7+)。问题是,设置tintColor
默认情况下不会执行任何操作:要使其工作,请使用
imageWithRenderingMode:
现在它可以工作:
文档链接。
性能
配置
UIImageView
后设置图像可避免重复昂贵的操作:Getting &异步设置图像可减少滚动和动画延迟(尤其是在
UICollectionViewCell
或UITableViewCell
内对图像进行着色时):If you only need it to look different, just use
imageView.tintColor
(iOS 7+). Catch is, settingtintColor
doesn't do anything by default:To make it work, use
imageWithRenderingMode:
And now it will work:
Link to documentation.
Performance
Setting the image after configuring the
UIImageView
avoids repeating expensive operations:Getting & setting the image asynchronously reduces scrolling and animation lag (especially when tinting an image inside of a
UICollectionViewCell
orUITableViewCell
):实现此目的的一种方法是降低图像的饱和度,并使用您想要的颜色在该图像上添加色调。
去饱和度
叠加颜色
操作方法
您可以将色调的颜色更改为您想要的任何颜色。
One way to accomplish this is to desaturate your image, and add a tint on top of that image with the color you desire.
Desaturate
Overlay With Color
How-To
You can change the color of the tint to whatever you desire.
这里有一篇关于此的很棒的帖子:
http://coffeeshopped.com/2010/09/iphone -how-to-dynamically-color-a-uiimage
我对当前代码的一个警告是,在视网膜图像上使用它会导致这些图像失去更高的“分辨率”。我目前正在寻找解决方案......
There's a great post about this here:
http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage
The one caveat that I have with the current code is that using it on retina images will result in a loss of the higher 'resolution' for these images. I am currently looking for a solution for this...
查看我的帖子(主要只是重新混合代码)< /a>.
编辑:此代码基本上创建一个新的
CGContext
,用新颜色在其上绘制一个图层,并从中返回一个新的UIImage
。我已经有一段时间没有深入研究这段代码了,但它似乎只是绘制了一个与原始形状相同的UIImage
,所以这是一个限制(丢失图像中的任何细节)。Check out my post (mostly just remixing code).
Edit: This code basically creates a new
CGContext
, draws a layer on it with the new color, and returns a newUIImage
from that. I haven't gone in depth on this code in a while, but it seems to just draw aUIImage
with the same shape as the original, so that's a limit (loses any detail in the image).如果您需要高性能,我强烈建议您使用
GPUImage
。您可以在 https://github.com/BradLarson/GPUImage 下载它
If you need high performance, I strongly recommend you to use
GPUImage
.You may download it at https://github.com/BradLarson/GPUImage
您正在操作的 RGB 数据只是一个副本。完成更改后,您需要将该数据转换回图像。
我首先制作一个新的位图:
并获取像素:
假设您的像素数据来自
pixels = CGBitmapContextGetData( ctx );
然后获取该上下文并从中构建一个新图像:The RGB data you are operating on is just a copy. After you finish making changes, you need to turn that data back into an image.
I first make a new bitmap:
And get the pixels:
Assuming that your pixel data came from
pixels = CGBitmapContextGetData( ctx );
then take that context and build a new image from it:我认为您可以创建另一个上下文,将上下文颜色设置为您想要为图片着色的 RGB。然后将您的 UIImage 绘制到该上下文中并使用该上下文,而不是直接使用您的图片。这是一个概念。这样您就可以使用彩色图像创建离屏缓冲区。我没有在可可中尝试过这个,只在碳中尝试过,但我想它也会以同样的方式起作用。
I think you can create another context with setting there context color to RGB you want to color your picture. Then draw your UIImage into that context and use that context instead of using directly your picture. This is a concept. This way you're creating offscreen buffer with a colored image. I didn't try this in cocoa, only in carbon, but i suppose it will work in the same way.
嗯——字节顺序不是应该是 RGBA 吗?您正在将它们设置为 ARGB...
Hmmm -- isn't the order of the bytes supposed to be RGBA? You are setting them as ARGB...
试试这个
try this
user576924 提到的很棒的帖子对我来说非常有用:
iPhone:如何动态为 UIImage 着色
和请
注意
,我还将“kCGBlendModeColorBurn”更改为“kCGBlendModeColor”,如帖子评论部分中所述。
The great post mentioned by user576924 worked great for me:
iPhone: How to Dynamically Color a UIImage
and in swift:
}
Note that I also changed "kCGBlendModeColorBurn" to "kCGBlendModeColor" as mentioned in the post's comments section.
对我来说这有效:
For me this worked: