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
tonyswu.mailingl...@gmail.com_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to