0

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?

1
  • Is there a reason you are closing the first context, and not just drawing the second image in the first context over / under the UIBezierPaths? Commented Feb 6, 2012 at 14:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.