MB.
MB.

Reputation: 4211

CGContext slow in iOS5

I'm using CGContext in two steps: First create context, draw background image, draw with UIBezierPaths, then get the image & release the context. Secondly combine this image with another one like this:

UIGraphicsBeginImageContextWithOptions(self.anchorImage.size, NO, 1);
[self.anchorImage drawInRect:CGRectMake(0, 0, self.anchorImage.size.width, self.anchorImage.size.height)];
[tempImage drawInRect:CGRectMake(0, 0, self.anchorImage.size.width, self.anchorImage.size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This worked well in iOS4, however it's very very slow in iOS5 (I'm testing on a 3GS). Am I doing something wrong? Or is there a more optimal way of doing this? Or is there a specific iOS5 way of doing it?

Upvotes: 0

Views: 925

Answers (1)

Joris Kluivers
Joris Kluivers

Reputation: 12081

It's not clear to me if you are just generating images, or drawing into a view. Anyway, to improve speed you could offload the generation of these images to a different dispatch queue, so your current thread (probably the main thread?) will not block.

Instead of UIGraphics* I would use CGBitmapContextCreate in combination with CGBitmapContextCreateImage. When your final image has been generated update the image view (or do whatever else you want to do with the image).

dispatch_async(your_async_queue, ^() {
    CGContextRef ctx = CGBitmapContextCreate(/* params */);

    // your drawing

    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);

    dispatch_async(dispatch_get_main_queue(), ^() {
        // do something with image on the main thread
    });
}

See the CGBitmapContext documentation for the full method signatures.

Upvotes: 2

Related Questions