0

我正在尝试在 UIImage 上叠加颜色,但仅在图像的左半部分(我使用来自http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color- a-uiimage覆盖颜色)。我现在的代码是:

- (UIImage *)imageWithColor:(UIColor *)color{

// begin a new image context, to draw our colored image onto
CGSize size = CGSizeMake(self.line.image.size.width/2, self.line.image.size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);

// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();

// set the fill color
[color setFill];

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

// set the blend mode to overlay, and the original image
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
CGContextDrawImage(context, rect, self.line.image.CGImage);

// set a mask that matches the shape of the image, then draw (overlay) a colored rectangle
CGContextClipToMask(context, rect, self.line.image.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);

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

//return the color-burned image
return coloredImg;
}

我认为将大小设置为宽度的一半会起作用,但一切仍然有颜色。我想我错过了一些非常基本的东西。有任何想法吗?

4

1 回答 1

0

CGContextAddRect(context, rect);

您正在添加一个全尺寸的矩形。

于 2013-04-27T22:14:33.247 回答