2016-01-17 24 views
-2

我想從圖像1複製到圖像2像素,並保存圖像1中的數據字典:[UIColor:CGPoint]從字典中逐個像素地從一個圖像繪製到另一個以相同的座標

如何在CGContext上繪製所有的像素點,並且圖像2上的某個像素在同一個CGPoint上的確切顏色?

@IBAction func save(sender: UIButton) { 
    UIGraphicsBeginImageContext(CGSizeMake(100, 50)) 
    let context = UIGraphicsGetCurrentContext() 
    for (color,point) in dict{ 
     drawPixel(context!, startPoint: point, color: color.CGColor) 
    } 
    testIV.image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 

func drawPixel(context:CGContextRef, startPoint:CGPoint, color:CGColorRef){ 
    dispatch_async(dispatch_get_main_queue(), { 
    CGContextSaveGState(context) 
    CGContextSetLineCap(context, .Square) 
    CGContextBeginPath(context) 
    CGContextSetStrokeColorWithColor(context, color) 
    CGContextSetLineWidth(context, 1.0) 
    CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5) 
    CGContextAddLineToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5) 
    CGContextStrokePath(context) 
    CGContextRestoreGState(context) 
    }) 
} 

我已經試過這樣,但圖像是空的...

+0

你嘗試過這麼遠嗎?顯示你自己的一些代碼,你想繪製的地方。 – luk2302

+0

我已經複製了照片中的像素,我想在同一個地方的另一張照片上繪製它 –

+0

是的,您對繪圖有什麼想法? – luk2302

回答

0

刪除調用dispatch_async。您希望您的繪圖代碼立即運行,但dispatch_async會導致它稍後運行,在您的save方法的其餘部分完成後很久。

func drawPixel(context:CGContextRef, startPoint:CGPoint, color:CGColorRef){ 
    CGContextSaveGState(context) 
    CGContextSetLineCap(context, .Square) 
    CGContextBeginPath(context) 
    CGContextSetStrokeColorWithColor(context, color) 
    CGContextSetLineWidth(context, 1.0) 
    CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5) 
    CGContextAddLineToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5) 
    CGContextStrokePath(context) 
    CGContextRestoreGState(context) 
} 

(這也是做的事情真的低效的方式,但是你會明白這一點很快...)

+0

現在它的作品,但一些像素丟失... –

+0

哪些?與其他點相比,這些點有什麼不同嗎?你需要自己調試。 –

+0

好吧我現在修復它如何把它放在圖像2在我從中獲取像素的相同位置? –

相關問題