How to set RGBA Color of a Pixel of an UIImage

爱⌒轻易说出口 提交于 2019-12-11 03:13:04

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/7547807/how-to-set-rgba-color-of-a-pixel-of-an-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!