2013-03-01 59 views

回答

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是一些很好的文章。

+0

此代碼將在視網膜顯示屏上繪製一個{2,2}大小的矩形。要繪製只有一個點,你應該調用CGContextFillRect(context,CGRectMake(x,y,1 ./(image.scale),1 ./(image.scale))) – gsempe 2013-03-01 09:02:53

+0

我們在哪裏設置像素的新顏色?請解釋。 – 2016-03-11 15:45:55

+0

@AvinashJadhav調用CGContextFillRect來做到這一點。在(x,y)處繪製一個尺寸爲1x1的矩形, – folex 2016-03-11 16:10:15