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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:55:55+00:00 2026-05-22T20:55:55+00:00

I’m writing a small painting app for iOS. I’m subclassing a UIView ad performing

  • 0

I’m writing a small painting app for iOS. I’m subclassing a UIView ad performing computations inside its drawRect: method. It’s been all good until I started having lots of objects (actually polylines), then performance started to degrade.

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef imageContext = UIGraphicsGetCurrentContext();

    int i, j;
    for(i = 0; i < [_strokes count]; i++) {
        NSMutableArray *stroke = [_strokes objectAtIndex:i];
        if([stroke count] < 2) {
            continue;
        }
        CGContextSetStrokeColorWithColor(imageContext, [[_penColours objectAtIndex:i] CGColor]);
        for(j = 0; j < [stroke count]-1; j++) {
            CGPoint line[] = {
                [[stroke objectAtIndex:j] CGPointValue],
                [[stroke objectAtIndex:j+1] CGPointValue]
            };

            CGContextSetLineWidth(imageContext, _brushSize);
            CGContextSetLineCap(imageContext, kCGLineCapRound);
            CGContextSetLineJoin(imageContext, kCGLineJoinRound);

            CGContextAddLines(imageContext, line, 2);
            CGContextStrokePath(imageContext);
        }
    }

    CGContextFlush(imageContext);
}

where _strokes is an array of arrays of points. My first idea was to make an image as an ivar, paint it on the context, paint the additional strokes to the context (change j = 0 with j = [stroke count] - 2), and grab the context to an image and store it back to the ivar. Didn’t work.

Then I tried many other roads not really worth mentioning, until I found this other question on SO ( Quartz2D performance – how to improve ). Unfortunately it didn’t work as I expected as I had to retain the image, resulting in memory warning 1, 2, crash.

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    CGContextRef imageContext = UIGraphicsGetCurrentContext();

    if(_image != nil) {
        CGContextDrawImage(imageContext, CGRectMake(0, 0, 1024, 768), [_image CGImage]);
    }

    int i, j;
    for(i = 0; i < [_strokes count]; i++) {
        NSMutableArray *stroke = [_strokes objectAtIndex:i];
        if([stroke count] < 2) {
            continue;
        }
        CGContextSetStrokeColorWithColor(imageContext, [[_penColours objectAtIndex:i] CGColor]);
        for(j = [stroke count]-2; j < [stroke count]-1; j++) {
            CGPoint line[] = {
                [[stroke objectAtIndex:j] CGPointValue],
                [[stroke objectAtIndex:j+1] CGPointValue]
            };

            CGContextSetLineWidth(imageContext, _brushSize);
            CGContextSetLineCap(imageContext, kCGLineCapRound);
            CGContextSetLineJoin(imageContext, kCGLineJoinRound);

            CGContextAddLines(imageContext, line, 2);
            CGContextStrokePath(imageContext);
        }
    }

    _image = UIGraphicsGetImageFromCurrentImageContext();
    [_image retain];
    UIGraphicsEndImageContext();

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, 1024, 768), [_image CGImage]);
}
  • 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-22T20:55:56+00:00Added an answer on May 22, 2026 at 8:55 pm

    It looks like at least part of the problem is that you retain _image but you never release it. Each time you assign a new value to _image, you’re leaking the previous image, and that’ll fill up memory very quickly.

    Beyond that, you should probably be using a CGLayer for your offscreen drawing rather than a bitmap. You can draw into the layer whenever you like, and then just copy it to the screen in your -drawRect: method.

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

Sidebar

Related Questions

I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.