0

我有一个滑块,我想根据滑块值使图像更亮或更暗。目前我有一个灰度图像,我在 viewDidLoad 上把它涂成红色。当我的滑块值改变时,我运行下面的代码。

我对代码的理解是我从文件中加载原始图像并将其存储在 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;
}
4

1 回答 1

0

我的错 - 问题在于我从哪里加载图像。

我需要使用:

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

不是

UIImage *myImage = [UIImage imageWithContentsOfFile:@"body.png"];
于 2013-04-03T12:08:47.743 回答