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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:47:40+00:00 2026-05-20T13:47:40+00:00

Can anyone explain why redrawing to an offscreen CGLayer would cause rendering to slow

  • 0

Can anyone explain why redrawing to an offscreen CGLayer would cause rendering to slow down over time? Let me show you a test I’ve created to illustrate the problem.

@implementation DrawView


- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //setup frame rate monitoring
        fps = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        fps.textColor = [UIColor whiteColor];
        fps.font = [UIFont boldSystemFontOfSize:15];
        fps.text = @"0 fps";
        [self addSubview:fps];
        frames = 0;
        lastRecord = [NSDate timeIntervalSinceReferenceDate];

        //create a cglayer and draw the background graphic to it
        CGContextRef context = UIGraphicsGetCurrentContext();
        cacheLayer = CGLayerCreateWithContext(context, self.bounds.size, NULL);

        CGImageRef background = [[UIImage imageNamed:@"background.jpg"] CGImage];
        CGContextRef cacheContext = CGLayerGetContext(cacheLayer);
        CGContextDrawImage(cacheContext, CGRectMake(0, 0, 768, 1024), background);

        //initialize cgimage stamp
        stamp = [[UIImage imageNamed:@"stamp.png"] CGImage];
        stampTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(stamp) userInfo:nil repeats:YES];
    }
    return self;
}

- (void) stamp {
    //calculate fps
    NSTimeInterval interval = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval diff = interval-lastRecord;
    if (diff > 1.0) {
        float rate = frames/diff;
        frames = 0;
        lastRecord = [NSDate timeIntervalSinceReferenceDate];
        fps.text = [NSString stringWithFormat:@"%0.1f fps", rate];
    }
    //stamp the offscreen cglayer with the cgimage graphic
    CGRect stampRect = CGRectMake(0, 0, 200, 200);
    CGContextRef cacheContext = CGLayerGetContext(cacheLayer);
    CGContextDrawImage(cacheContext, stampRect, stamp);
    [self setNeedsDisplayInRect:stampRect];
}

- (void)drawRect:(CGRect)dirtyRect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLayerInRect(context, self.bounds, cacheLayer);
    frames++;
}

When I run this test in the ipad simulator or device it starts at 40 fps and drops at a constant rate over the course of 10 seconds until it’s running at like 3 fps. Why is this happening? Shouldn’t this run at a constant framerate? What kind of solution would allow me to ‘stamp’ an image over and over while maintaining a constant framerate?

  • 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-20T13:47:41+00:00Added an answer on May 20, 2026 at 1:47 pm

    Your problem is that you created your layer with a NULL context. UIGraphicsGetCurrentContext() is only valid during the draw loop. You call it outside the draw loop so it’s NULL and so the layer can’t cache anything. It still surprises me how badly this trashes performance over time. I suspect there might be a bug in CoreGraphics; you’d think this would just be slow; not “ever slowing.” But still, it’s not designed to work this way.

    If you create a bitmap context and use that for your layer, you’ll get your 60fps. You don’t need to abandon CGLayer here.

    All I did to get back to 60fps is to replace this:

    CGContextRef context = UIGraphicsGetCurrentContext();
    

    with this:

    CGContextRef context = CreateBitmapContext(self.bounds.size);
    

    Where CreateBitmapContext() is a function that returns the same thing your createBitmapContext sets up.

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

Sidebar

Related Questions

Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you
Can anyone explain the difference between the types mentioned above and some sample usage
Can anyone explain exactly how the Strategy Pattern relates to Inversion of Control?

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.