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
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents