Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw a shape on top of a UIImage while respecting the image's alpha mask
    primarykey
    data
    text
    <p>I need a UIImageView that can draw itself in color or b/w according to a flag:</p> <pre><code> BOOL isGrey; </code></pre> <p>I'm trying to do it by drawing a black rectangle on top of the original image with the Quartz blendmode set to Color. This works except it doesn't respect the image's alpha mask.</p> <p>See illustration: <a href="http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png">alt text http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png</a></p> <p>Searching Google and SO, I found and tried several solutions but none respect the mask either.</p> <p>Here is the code that produces the 'What I get' image above:</p> <pre><code> - (void)drawRect:(CGRect)rect { if (isGrey) { CGContextRef context = UIGraphicsGetCurrentContext(); // flip orientation CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); // draw the image CGContextDrawImage(context, self.bounds, self.image.CGImage); // set the blend mode and draw rectangle on top of image CGContextSetBlendMode(context, kCGBlendModeSaturation); CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); CGContextFillRect(context, rect); } else { [self.image drawInRect:rect]; } } </code></pre> <p>Is there some Quartz drawing mode that I'm forgetting to set? I've looked thru the Quartz Programming Guide but is so hard to extract the one bit of info you need from the overlapping and hyperlinked subjects.</p> <p><em>Obviously I'm looking for a general solution that will apply to images with any masked shape, not just the circle shown.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload