【问题标题】:iOS - Color an image multiple times from original sourceiOS - 从原始来源多次为图像着色
【发布时间】:2013-04-03 11:58:05
【问题描述】:

我有一个滑块,我想根据滑块值使图像更亮或更暗。目前我有一个灰度图像,我在 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;
}

【问题讨论】:

    标签: iphone ios ipad cgcontext


    【解决方案1】:

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

    我需要使用:

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

    不是

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

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2023-03-07
      • 2022-01-09
      相关资源
      最近更新 更多