bryan
bryan

Reputation: 2243

Why is CGContextDrawImage in drawRect initially slow?

Consider this simple UIView subclass in an ARC-enabled iOS app that draws on the view when the screen is touched:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint location = [[touches anyObject] locationInView:self];

    int padding = brushSize / 2;

    CGRect brushRect = CGRectMake(
        location.x - padding, location.y - padding,
        brushSize, brushSize
    );

    [self setNeedsDisplayInRect:brushRect];

}

- (void)drawRect:(CGRect)rect {

    NSDate *start = [NSDate date];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, brushImage);

    NSLog(@"%f", [start timeIntervalSinceNow]);

}

In this example, brushImage is a square bitmap, and brushSize is an int describing the width and height of the bitmap.

I consistently get log output like this:

-0.131658
-0.133998
-0.143314
-0.007132
-0.006968
-0.007444
-0.006733
-0.008574
-0.007163
-0.006560
-0.006958

The first calls to drawRect take much longer to complete than subsequent calls, and drawRect continues to be consistently fast thereafter. It's only on the first calls after the view is initialized that drawRect is slow.

I see similar results in both the simulator and all physical devices I've tried. The initial calls always take much longer to complete.

Why is drawRect / CGContextDrawImage so slow on the initial calls? And more importantly, how can I fix this?

Upvotes: 3

Views: 901

Answers (1)

borrrden
borrrden

Reputation: 33423

Check the rect. I bet the first three are fullscreen updates. I ran into this issue before. IOS will forcibly update the first few times as fullscreen no matter what you do (probably for its GL buffering system). If you leave it alone for about 5 seconds, and then draw something again you will get the same behavior. I never figured out what the actual cause was, and I doubt I ever will :(.

See the question I originally asked: UIGraphicsGetCurrentContext() short lifetime

Upvotes: 3

Related Questions