2013-04-03 31 views
0

我有一個滑塊,我希望根據滑塊值使圖像更明亮或更暗。目前我有一個灰度圖像,並且我在viewDidLoad上將它着色爲紅色。當我的滑塊的值發生變化時,我運行下面的代碼。iOS - 從原始來源多次對圖像進行着色

我對代碼的理解是,我從文件加載原始圖像並將其存儲在myImage中。然後設置填充顏色,然後使用CGContextDrawImage將myImage繪製到圖形上下文中。然後根據混合模式集合填充圖像,然後將新圖像設置爲self.bodyView.image(這是在超級視圖中可見的圖像)。

似乎正在發生的事情是,不是從文件重新加載body.png,它看起來像只是將效果重新應用到已經改變的圖像。爲什麼是這樣?

在此先感謝。

- (IBAction)bodyBrightnessChanged:(UISlider *)sender { 

... 
code to set 3 floats called red, green and blue to be values between 0 and 1 
... 

UIImage *myImage = [UIImage imageWithContentsOfFile:@"body.png"]; 

// Begin a new image context to draw the coloured image onto 
UIGraphicsBeginImageContext(self.bodyView.image.size); 

// Get a reference to the context we created 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// Set the fill colour 
[[UIColor colorWithRed:red green:green blue:blue alpha:1.0] setFill]; 

// translate/flip the graphics context (for transforming from CG* coords to UI* coords 
CGContextTranslateCTM(context, 0, self.bodyView.image.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

// set the blend mode and the original image 
CGContextSetBlendMode(context, kCGBlendModeColor); 
CGRect rect = CGRectMake(0, 0, self.bodyView.image.size.width, self.bodyView.image.size.height); 
//CGContextDrawImage(context, rect, self.bodyView.image.CGImage); // This is where we need to set the original image - I think... 
CGContextDrawImage(context, rect, myImage.CGImage); 

// Set a mask that matches the shape of the image, then draw (colour burn) a coloured rectangle 
CGContextClipToMask(context, rect, self.bodyView.image.CGImage); 
CGContextAddRect(context, rect); 
CGContextDrawPath(context, kCGPathFill); 

// Generate a new UIImage from the graphics context we drew onto 
UIImage *colouredImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

self.bodyView.image = colouredImage; 
} 

回答

0

我的不好 - 問題是我從哪裏加載圖像。

我需要使用:

UIImage *myImage = [UIImage imageNamed:@"body.png"]; 

UIImage *myImage = [UIImage imageWithContentsOfFile:@"body.png"];