如何设置UIImage的像素的RGBA颜色 [英] How to set RGBA Color of a Pixel of an UIImage

查看:282
本文介绍了如何设置UIImage的像素的RGBA颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个可能性来设置(更改)我的Iphone应用程序的UIImage像素的RGBA颜色。 (我实际上想实现一些floodfill的东西)



所以我需要这样:

   - (void)setPixelColorOfImage:(UIImage *)img atXCord:(int)x atYCord:(int)y 
withRedColor:(CGFloat)red blueColor:(CGFloat)blue greenColor:(CGFloat) green
alpha:(CGFloat)alpha {

//需要一些代码her

}

因为这个方法会被频繁调用,所以不应该太慢。



请帮助。

解决方案

UIImage不是,所以你必须复制,然后编辑,然后转换为UIImage。



复制




  • 创建CGBitmapContext

  • 在其上绘制源图片






  • 获取上下文的像素缓冲区

  • 修改像素缓冲区



转换为UIImage




  • 复制CGImage表示上下文

  • 从CGImage



fwiw创建一个新的UIImage,在osx上使用(使用NSImage)。


I am looking for a possibility to set (change) the RGBA color of a Pixel of an UIImage for my Iphone Application. (I actually want to implement some floodfill stuff)

So I need something like this:

-(void) setPixelColorOfImage: (UIImage*) img atXCord: (int) x atYCord: (int) y
withRedColor: (CGFloat) red blueColor: (CGFloat) blue greenColor: (CGFloat) green
alpha: (CGFloat) alpha  {

// need some code her

}

Because this method will be called quite often, it shouldn't be too slow.

Please help.

解决方案

UIImage is not (externally) mutable, so you must copy, then edit, then convert to UIImage.

Copy

  • create a CGBitmapContext
  • draw the source image onto it

Edit

  • get a hold of the context's pixel buffer
  • mutate that pixel buffer

Convert to UIImage

  • copy a CGImage representation of the context
  • create a new UIImage from the CGImage

fwiw, the form can also be used on osx (using NSImage).

这篇关于如何设置UIImage的像素的RGBA颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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