After browsing through all the relevant question on SO, I understand drawViewHierarchyInRect is supposed to be much faster than renderInContext, yet this is not what I am seeing.
Here is my snapshot function with time profiling:
- (UIImage *)screenshot
{
UIScreen *mainScreen = [UIScreen mainScreen];
CGSize imageSize = mainScreen.bounds.size;
if (&UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 1);
} else {
UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in windows) {
if (![window respondsToSelector:@selector(screen)] || window.screen == mainScreen) {
NSDate *methodStart = [NSDate date];
// [window.layer renderInContext:context];
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);
}
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Tests were done on iPad Air with IOS 9.0 As a test app, I am using UIKitCatalog application https://developer.apple.com/library/ios/samplecode/UICatalog/Introduction/Intro.html
The app shows 2 windows:
<UIWindow: 0x134e4d8f0; frame = (0 0; 768 1024); gestureRecognizers = <NSArray: 0x134e27740>; layer = <UIWindowLayer: 0x134e4d130>>,
<UITextEffectsWindow: 0x134e6a3a0; frame = (0 0; 768 1024); opaque = NO; autoresize = W+H; layer = <UIWindowLayer: 0x134e6aba0>>
Using drawViewHierarchyInRect results in 0.078s on any of the two windows renderInContext results in 0.045s on the main window and some smaller (0.000028s) time on the UITextEffectsWindow.
Why is drawViewHierarchyInRect slower than renderInContext, isn't it supposed to be faster
Both (78ms, 45ms) are unacceptable for recording video of the screen with any decent frame rate. Seems like something is very wrong, it should be much faster.
[window.layer renderInContext...] results in screenshots which are messy and inaccurate, I've tried [window.layer.presentationLayer renderInContext...], yet this results in application crashes and I can't find the reason.
I would appreciate any help with this, all I am trying to do is record a video of the screen at the highest frame rate possible and so far the results I am getting do not make sense or I might be doing something very wrong.
Thanks