Getting the Snapshot
suggest change- (UIImage *)getSnapshot
{
UIScreen *screen = [UIScreen mainScreen];
CGRect bounds = [self.view bounds];
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, screen.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Swift 3
This computed property belongs on a UIView. It renders the layer, which can miss content that drawViewHierarchyInRect:afterScreenUpdates: includes. The context and the image can be nil, so the result is optional. UIGraphicsBeginImageContext uses a scale of 1.
var screenshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
layer.render(in: context)
let screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenShot
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents