一癞季、 創(chuàng)建UIWebView
// 創(chuàng)建一個(gè)webView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
webView.backgroundColor = [UIColor whiteColor];
webView.opaque = NO;
webView.delegate = self;
webView.scalesPageToFit = YES; // 頁(yè)面大小適應(yīng)屏幕
[self.view addSubview:webView];
self.webView = webView;
二绷柒、加載web鏈接
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
三废睦、在webView代理方法webViewDidFinishLoad頁(yè)面加載完成的時(shí)候养泡,截圖
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
UIScrollView *scroll = self.webView.subviews.firstObject;
scroll.frame = scroll.superview.frame;
CGRect frm = scroll.frame;
frm.size.height = _webView.scrollView.contentSize.height;
scroll.frame = frm;
[scroll.superview layoutIfNeeded];
// 執(zhí)行截圖
[self screenView:scroll];
}
四、截圖方法
/// 截圖
- (void)screenView:(UIScrollView *)view
{
// 設(shè)置截圖大小
UIGraphicsBeginImageContextWithOptions(CGSizeMake(SCREEN_WIDTH,view.frame.size.height), YES, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = viewImage.CGImage;
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRef];
NSLog(@"sendImage==%@",sendImage);
//保存圖片到照片庫(kù) (iOS10 以上記得在info.plist添加相冊(cè)訪(fǎng)問(wèn)權(quán)限净蚤,否則可能崩潰)
UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);
}
五今瀑、 按Home鍵点把,到手機(jī)系統(tǒng)相冊(cè)查看截圖橘荠。完成。