簡體   English   中英

如何更改uiimage中單個像素的顏色

[英]how to change the color of an individual pixel in uiimage

我有一個UIImage,它是一個山地景觀的圖像。 我需要通過將相應的2-3像素點變為紅色來顯示此圖像上的位置。 我該如何完成? 謝謝!

您可以像這樣在圖形上下文中繪制圖像:

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

您還應該注意圖像的方向。 這里有一些不錯的文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM