2012-10-22 71 views
0

在一個類中,我有一個包含五個UIImage(例如tiangle.png和circle.png)的數組。用掩碼更改UIImage的顏色並用彩色圖像替換該圖像

在當前的課程中,我有一個顏色列表。點擊UIImage後,點擊一種顏色後,可以更改當前UIImage的顏色。 這將通過掩蓋圖像,設置其顏色並用所選顏色中的新UIImage替換舊UIImage來實現。

但有一些錯誤的方法,它應該改變顏色:

- (void) changeColor: (UITapGestureRecognizer*) gestureRecognizer{ 

    UIGraphicsBeginImageContext(test.newView.image.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGPoint loc = [gestureRecognizer locationInView:self.view]; 
    CGContextSetFillColorWithColor(context, [[self colorOfPoint:loc]CGColor]); 
    CGContextTranslateCTM(context, 0, test.newView.image.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGRect rect = CGRectMake(test.newView.frame.origin.x,test.newView.frame.origin.x, test.newView.image.size.width,test.newView.image.size.height); 
    CGContextSetBlendMode(context, kCGBlendModeColorBurn); 

    CGContextClipToMask(context, rect, test.newView.image.CGImage); 
    CGContextAddRect(context, rect); 
    CGContextDrawPath(context, kCGPathFill); 

    CGImageRef imgRef = CGBitmapContextCreateImage(context); 
    UIImage* img = [UIImage imageWithCGImage:imgRef]; 
    CGImageRelease(imgRef); 
    CGContextRelease(context); 
    UIGraphicsEndImageContext(); 
    test.newView.image = img; 

} 

正在發生的事情的唯一的事情就是點擊UIImage的是會得到不透明。
持有UIImage的imageview在這種情況下不會被刪除。

回答

1

我不確定在UIGraphics圖像上下文中使用CGBitmapContextCreateImage()調用來獲取imageRef是否合適。我見過的每個示例都使用了

UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); 

在關閉圖像上下文之前抓取圖像。你可以試試。

+0

是的,謝謝,這是正確的,然後將其值傳遞給UIImageView的實際圖像:newView.image = image; – Studie