Recently I tried to capture a screenshot of a UIWebView with animating elements. The problem is that they appear on the screenshot at their original positions, like there was no animation. I tried to use the following code to fix this problem:
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(context, screenRect);
[[self.layer presentationLayer] renderInContext:context];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
But it drops my application down with an EXC_BAD_ACCESS.
A little research showed that this behavoir is due to presentationLayer that changes swiftly; so when I try to renderInContext, it actually doesn't exist, and the [self.layer presentationLayer]
now returns another CALayer object.
So the question is how to take an image from the current state of an animating layer, or (this also could be a solution) how to stop it from changing the presentationLayer for the time of screenshooting?
[update] More interesting: presentationLayer seems to be empty, since it's contents is nil, as I can see in the debugger...
Okay, the problem was in CSS:
-webkit-transform: perspective(100px) translate3d(200px, 100px, -50px) rotateY(15deg);
Any of perspective or rotateY/rotateX kills the position of the element on the screenshot, taken from view.layer renderInContext. So there's no way to capture the "really 3d" positioned elements on the webpage.