WKWebView Screenshots
Implement in WKWebView(Emulator)
UIWebView and WKWebView
To make screenshots of WKWebView, we need to use snapshotViewAfterScreenUpdates
※ This codes work only for Simulator.
If you want to save it as Image, you need to save image directly. See next section.
swift Extension
Capture.swift
import Foundation import WebKit extension WKWebView { func screenCapture() -> UIImage { var capturedView : UIView? = self.snapshotViewAfterScreenUpdates(false) var image : UIImage? = nil; if (capturedView != nil) { let size = self.scrollView.contentSize; UIGraphicsBeginImageContextWithOptions(size, true, 0); let rect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, size.width, size.height) capturedView?.drawViewHierarchyInRect(rect, afterScreenUpdates: true); image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return image!; } func screenCapture(size: CGSize) -> UIImage { var capturedView : UIView? = self.snapshotViewAfterScreenUpdates(false) var image : UIImage? = nil; if (capturedView != nil) { UIGraphicsBeginImageContextWithOptions(size, true, 0); let ctx = UIGraphicsGetCurrentContext(); let scale : CGFloat! = size.width / capturedView!.layer.bounds.size.width; let transform = CGAffineTransformMakeScale(scale, scale); CGContextConcatCTM(ctx, transform); capturedView?.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true); image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return image!; } }
Objective-C Category
WKWebView+Capture.m
- (UIImage *)screenCapture { UIView *capturedView = [self snapshotViewAfterScreenUpdates:NO]; UIImage *image = nil; if (capturedView) { CGSize size = self.scrollView.contentSize; UIGraphicsBeginImageContextWithOptions(size, YES, 0); // [capturedView drawViewHierarchyInRect:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, size.width, size.height) afterScreenUpdates:YES]; // Change this is the good way [self.layer renderInContext:UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return image; } - (UIImage *)screenCapture:(CGSize)size { UIView *capturedView = [self snapshotViewAfterScreenUpdates:NO]; UIImage *image = nil; if (capturedView) { UIGraphicsBeginImageContextWithOptions(size, YES, 0); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGFloat scale = size.width / capturedView.layer.bounds.size.width; CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale); CGContextConcatCTM(ctx, transform); //[capturedView drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; [self.layer renderInContext:UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return image; }
Device
Above code doesn’t work in an actual device.
Write image directly from WKWebView.
Objective-C Category
WKWebView+Capture.m
- (UIImage *)screenCapture { CGSize size = self.scrollView.contentSize; UIGraphicsBeginImageContextWithOptions(size, YES, 0); // [self drawViewHierarchyInRect:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, size.width, size.height) afterScreenUpdates:YES]; [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } - (UIImage *)screenCapture:(CGSize)size { UIGraphicsBeginImageContext(size); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGFloat scale = size.width / self.layer.bounds.size.width; CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale); CGContextConcatCTM(ctx, transform); // [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO]; [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }