iOS 使用WKWebView的截图功能来截取H5页面,并将截图保存到相册。

swift版本

import WebKit
import Photos

// 在您的视图控制器中
class ViewController: UIViewController, WKNavigationDelegate {
    // 声明一个WKWebView实例
    var webView: WKWebView!
    
    // ...
    
    // 在合适的地方初始化并加载H5页面
    func loadWebView() {
        webView = WKWebView(frame: view.bounds)
        webView.navigationDelegate = self
        view.addSubview(webView)
        
        let url = URL(string: "https://example.com")!
        let request = URLRequest(url: url)
        webView.load(request)
    }
    
    // 实现WKNavigationDelegate的方法,以便在页面加载完成后进行截图操作
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        takeScreenshot()
    }
    
    // 截图并保存到相册
    func takeScreenshot() {
        let scrollView = webView.scrollView
    	let originalContentOffset = scrollView.contentOffset
    	let originalContentSize = scrollView.contentSize
    
    	scrollView.contentOffset = .zero
    	scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: webView.bounds.size.height)
    
    	UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, 0.0)
    	guard let context = UIGraphicsGetCurrentContext() else {
        	completion(nil)
        	return
    	}
    
    	for subview in scrollView.subviews {
        	context.saveGState()
        	context.translateBy(x: subview.frame.origin.x - scrollView.contentOffset.x, y: subview.frame.origin.y - scrollView.contentOffset.y)
        	subview.layer.render(in: context)
        	context.restoreGState()
    	}
    
    	let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    	UIGraphicsEndImageContext()
    
    	scrollView.contentOffset = originalContentOffset
    	scrollView.contentSize = originalContentSize
    
    	UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
    	completion(screenshot)

    }
    
    // ...
}

请注意,为了将截图保存到相册,您需要在Info.plist文件中添加相册访问权限的描述。将以下内容添加到Info.plist文件中的标签中:

<key>NSPhotoLibraryAddUsageDescriptionkey>
<string>需要保存截图到相册string>

OC版本

#import <WebKit/WebKit.h>
#import <Photos/Photos.h>

// 在您的视图控制器中
@interface ViewController () <WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化并加载H5页面
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    
    NSURL *url = [NSURL URLWithString:@"https://example.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self takeScreenshot];
}

- (void)takeScreenshot {
    UIScrollView *scrollView = self.webView.scrollView;
    CGPoint originalContentOffset = scrollView.contentOffset;
    CGSize originalContentSize = scrollView.contentSize;
    
    scrollView.contentOffset = CGPointZero;
    scrollView.contentSize = CGSizeMake(self.webView.bounds.size.width, [self.webView.scrollView contentSize].height);
    
    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    for (UIView *subview in scrollView.subviews) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, subview.frame.origin.x - scrollView.contentOffset.x, subview.frame.origin.y - scrollView.contentOffset.y);
        [subview.layer renderInContext:context];
        CGContextRestoreGState(context);
    }
    
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    scrollView.contentOffset = originalContentOffset;
    scrollView.contentSize = originalContentSize;
    
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
}

@end

同样,在Info.plist文件中添加相册访问权限的描述。在标签中添加以下内容:

<key>NSPhotoLibraryAddUsageDescriptionkey>
<string>需要保存截图到相册string>

你可能感兴趣的:(swift,ios,swift,objective-c)