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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:35:51+00:00 2026-06-08T17:35:51+00:00

i need to improve my drawRect method. It´s so extremely slow. I have 10

  • 0

i need to improve my “drawRect” method. It´s so extremely slow.
I have 10 transparent png´s which i want to merge together with different blend modes.
I´ve read a post from the Apple Developer Lib and from Stackoverflow.

Now i want to know how to improve my code and the overall drawing performance.
Could you please help me? Loading all these images within a for loop makes it so slow, right?
How do i get the most performance?

  - (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGAffineTransform matrix =CGContextGetCTM(context);
        CGAffineTransformInvert(matrix);
        CGContextConcatCTM(context,matrix);

            CGRect contentRect;

             // load each UIImage
        for (i=0; i< [configurationArray count]; i = i + 1) {   
                    image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];

    contentRect = CGRectMake(x,y,image.size.width,image.size.height);
    CGContextSetBlendMode (context, kCGBlendModeScreen);
    CGContextSetAlpha (context, [layerData.layerAlpha floatValue]);


    CGContextDrawImage (context,contentRect, image.CGImage);

    }
}

UPDATE:

Now i´m using CALayers to put the pixel direct on the needed position.
But i need different blend modes, alpha and color fills for each layer.
Is there a way to avoid calling the expensive “CGContextDrawImage” method on each layer?
E.g i hope its possible to just update the context for the small size of the specific layer, right?
Or do i need to render the whole UIView for each context change?

I hope somebody can push me in the right direction.
I just have to learn on that topic.
Thanks for any help.

- (void)drawRect:(CGRect)rect {
 // load each UIImage
        for (int i=0; i< [myArray count]; i++) { 

        image = [[ImageCache sharedImageCache] imageForKey:imageName];

       CALayer* sublayer = [CALayer layer] ;

        [sublayer setPosition:CGPointMake(image.size.width/2, image.size.height/2)]; 
        [sublayer setFrame:CGRectMake(x, y, image.size.width, image.size.height)];

        CGImageRef layerImage = [image CGImage];
        [sublayer setContents:(__bridge id) layerImage];
        [sublayer setBackgroundColor:[[UIColor clearColor] CGColor]];
        [self.layer addSublayer:sublayer];

// How can i set the blend mode, color fill & alpha value without 
// calling the expensive "CGContextDrawImage" ? Possible?

}
}
  • 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-08T17:35:52+00:00Added an answer on June 8, 2026 at 5:35 pm

    Per Brad’s comment:

    You probably shouldn’t load your ten images within -drawRect:. Load and cache them somewhere else first. Be careful about memory usage with that many images, though.

    I´ve ended with Brad’s approach.

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

Sidebar

Related Questions

I have legacy code which I need to improve for performance reasons. My application
I have recently started to feel that I need to greatly improve my C++
I have a .Net 4.0 application that I need to improve the performance of
I have one jQuery code and need some help from u guys to improve
I need to improve the search on a website which has a search box
I want to improve my coding skills, so I have planned write a Mobile
I have no real need to improve it, it's just for fun. Right now
I have 200+ uiLabels on a long scrollview, I need to improve scrolling performance.
I need to improve the response time for a Grails application, so I need
I need to improve on a regular expression I'm using. Currently, here it is:

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.