如何更改uiimage中单个像素的颜色 [英] how to change the color of an individual pixel in uiimage

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

问题描述

我有一个 UIImage,它是一个山地景观的图像.我需要通过将相应的 2-3 个像素点变成红色来显示此图像上的位置.我该如何实现?谢谢!

I have a UIImage which is an image of a mountainous landscape. I need to show locations on this image, by turning corresponding 2-3 pixels at the spot to red. How do I accomplish this? Thanks!

推荐答案

您可以像这样将图像绘制到图形上下文:

You can draw image to graphics context like this:

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

然后像这样在任何地方画一个点:

And then just draw a point on it wherever you want like this:

//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:

Then just save it to UIImage again:

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

您还应该注意图像的方向.这里是一些关于它的好文章.

You also should take care of image's orientation. Here is some good article on it.

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

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