In my app I'm using a screenshot method. On my iPad 2 it's very fast (about 130 ms) to execute this method. But on the new iPad (certainly due to the highest resolution and the same CPU) it's taking like 700 ms ! Is there a way to optimize my method ? Perhaps there's a way to work directly with graphic card ?
Here's my screenshot method :
- (UIImage *)image {
CGSize imageSize = self.bounds.size;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
else UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, [self center].x, [self center].y);
CGContextConcatCTM(context, [self transform]);
CGContextTranslateCTM(context, -[self bounds].size.width * [[self layer] anchorPoint].x, -[self bounds].size.height * [[self layer] anchorPoint].y);
[[self layer] renderInContext:context];
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Thanks for your help.
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0f);
instead of UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
.
This will make for 1:1 screenshot. Or is there a specific reason you need [UIScreen mainScreen].scale
there - Rok Jarc 2012-04-04 07:26
I think it was the developer of Flipboard talking about this issue on a podcast. It is a real issue with the iPad 3, because they've quadrupled the pixels.
What he was doing was taking the screenshot ahead of time in the background, not when the user initiated the action - in his case when the user "flipped" the page.
I don't know whether this will help you in your case, but it is certainly a viable approach for many cases.
This may or may not be sufficient for your application but one option is to scale the screenshot down, e.g. pass 0.5 as your scale factor to UIGraphicsBeginImageContextWithOptions. 4x faster but the tradeoff is a loss of detail/resolution.