UIImage Color Overlay
UIImage Color Overlay
- Subject: UIImage Color Overlay
- From: Tony S.Wu <email@hidden>
- Date: Wed, 21 Jul 2010 12:23:23 -0700
Hi,
I have a UIView with a UIImage as background, and I would like to overlay the UIImage with different colors according to user preferences. Right now I draw the image over the background, and apply the color overlay:
UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
[backgroundImage drawInRect:CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height)];
CGRect bounds = [self bounds];
[[UIColor blueColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, bounds, [backgroundImage CGImage]);
CGContextFillRect(context, bounds);
And that works just fine. However, it's come to a point that I would like to approach this from another angle. I would like to apply the color overlay over the background image, and use the resulting image to draw over the view. I have my code as:
UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
CGSize size = backgroundImage.size;
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIColor *color = [UIColor blueColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, backgroundImage.CGImage);
CGContextSetBlendMode (context, kCGBlendModeColor);
CGContextClipToMask(context, rect, backgroundImage.CGImage);
CGContextSetFillColor(context, CGColorGetComponents(color.CGColor));
CGContextFillRect (context, rect);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRestoreGState(context);
UIImage *image = [UIImage imageWithCGImage:imageMasked];
[image drawInRect:CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height)];
But when I do this, the result aren't quite the same, and the image only draws over part of the view. Any idea? Thanks.
Tony S. Wu
email@hidden_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden