收录日期:2019/05/24 23:32:52 时间:2012-10-09 13:03:21 标签:iphone,graphics

I am using the following code to fill color in an image in iPhone.

- (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
UIGraphicsBeginImageContext(baseImage.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);

CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, baseImage.CGImage);

[theColor set];
CGContextFillRect(ctx, area);

CGContextRestoreGState(ctx);

CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

CGContextDrawImage(ctx, area, baseImage.CGImage);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

But the above code fills the color in the whole image. I just want to feel upper part, say 1/4th of the image. So how to do it?

I figured it out.. change the line of CGContextFillRect to. CGContextFillRect(ctx, CGRectMake(0, 0, baseImage.size.width, 100));