Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 350953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:35:54+00:00 2026-05-12T11:35:54+00:00

I need a UIImageView that can draw itself in color or b/w according to

  • 0

I need a UIImageView that can draw itself in color or b/w according to a flag:

  BOOL isGrey;

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.

See illustration:
alt text http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png

Searching Google and SO, I found and tried several solutions but none respect the mask either.

Here is the code that produces the ‘What I get’ image above:

 - (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];
    }
}

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.

Obviously I’m looking for a general solution that will apply to images with any masked shape, not just the circle shown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:35:54+00:00Added an answer on May 12, 2026 at 11:35 am

    To draw a shape while respecting an image’s alpha mask, just add one line before you draw:

     CGContextClipToMask(context, self.bounds, image.CGImage);
    
     // example usage
      - (void)drawRect:(CGRect)rect {
    
        if (isGrey) {
                CGContextRef context = UIGraphicsGetCurrentContext();
    
                // flip orientation
                CGContextTranslateCTM(context, 0.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, kCGBlendModeColor);
                CGContextClipToMask(context, self.bounds, image.CGImage); // respect alpha mask
                CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
                CGContextFillRect(context, rect);               
        } else {
                [self.image drawInRect:rect];
        }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subclass of UIImageView that I need to know when is touched.
I have an array of images (can be up to 25) that need to
I'm trying to render some UIImages into 1 single image that I can save
I need to draw a pie chart. I found a nice tutorial here http://mac-objective-c.blogspot.com/2009/04/drawing-pie-charts.html
I am developing one applciation.In that I need to display the 9 UIImageViews in
I have a UIImageView subclass and I need to have a pan gesture so
I have an UIImageView and taking the raw touch input. I need to check
I have a UIScrollView which has an UIImageView in it. I need to be
Need a function that takes a character as a parameter and returns true if
I have a custom UIImageView class that creates thumbnails (UIImageView) and each thumbnail has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.