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 8457661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:51:57+00:00 2026-06-10T12:51:57+00:00

I may point out that Drawing and Rendering in Objective-C is my weakness. Now,

  • 0

I may point out that Drawing and Rendering in Objective-C is my weakness. Now, here’s my problem.

I want to add a ‘Day/Night’ feature to my game. It has got lots of objects on a map. Every object is a UIView containing some data in variables and some UIImageViews: the sprite, and some of the objects have a hidden ring (used to show selection).

I want to be able to darken the content of the UIView, but I can’t figure out how. The sprite is a PNG with transparency. I’ve just managed to add a black rectangle behind the sprite using this:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);

CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.5);
CGContextFillRect(ctx, rect);
CGContextRestoreGState(ctx);

As I’ve read, this should be done in the drawRect method. Help please!

If you want to understand better my scenario, the App where I’m trying to do this is called ‘Kipos’, at the App Store.

  • 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-06-10T12:51:58+00:00Added an answer on June 10, 2026 at 12:51 pm

    Floris497’s approach is a good strategy for a blanket darkening for more than one image at a time (probably more what you’re after in this case). But here’s a general purpose method to generate darker UIImages (while respecting alpha pixels):

    + (UIImage *)darkenImage:(UIImage *)image toLevel:(CGFloat)level
    {
        // Create a temporary view to act as a darkening layer
        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        UIView *tempView = [[UIView alloc] initWithFrame:frame];
        tempView.backgroundColor = [UIColor blackColor];
        tempView.alpha = level;
    
        // Draw the image into a new graphics context
        UIGraphicsBeginImageContext(frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [image drawInRect:frame];
    
        // Flip the context vertically so we can draw the dark layer via a mask that
        // aligns with the image's alpha pixels (Quartz uses flipped coordinates)
        CGContextTranslateCTM(context, 0, frame.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextClipToMask(context, frame, image.CGImage);
        [tempView.layer renderInContext:context];
    
        // Produce a new image from this context
        CGImageRef imageRef = CGBitmapContextCreateImage(context);
        UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        UIGraphicsEndImageContext();
        [tempView release];
        return toReturn;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope somebody may be able to point out where i'm going wrong here
It is quite possible that I may not have got the point, but I
Suppose you have a point cloud, and you want a surface that wraps those
Okay, I have a feeling that you guys'll be able to quickly point out
This may have been answered before, and if so, point me to the answer.
May I install a search provider/accelerator remotely in Internet Explorer 8? I dont want
May be obvious but I tried searching and can't figure it out. I am
I have a panel that may or may not be within other panels. I'm
I have a problem with drawing a polygon in Java applet. I don't understand
Can anyone point out a turorial which explains threading?? In my application, i'm uploading

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.