如何更改uiimage中单个像素的颜色

时间:2013-03-01 07:44:10

标签: ios uiimage

我有一张UIImage,是山地景观的图像。我需要在此图像上显示位置,方法是将该位置的相应2-3个像素转换为红色。我该如何做到这一点?谢谢!

1 个答案:

答案 0 :(得分:4)

您可以将图像绘制到图形上下文中,如下所示:

CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);
CGContextDrawImage(context, imageRect, image.CGImage);

然后在任何你想要的地方画上一点:

//CGContextFillRect(context, CGRectMake(x,y,1,1));
//Fix error according to @gsempe's comment
CGContextFillRect(context, CGRectMake(x,y,1./(image.scale),1./(image.scale)))

然后再将它保存到UIImage:

CGContextRestoreGState(context);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

您还应该注意图像的方向。 Here是一篇很好的文章。