需要截取wkwebview的內(nèi)容成圖片,保存或者分享出去;
建分類,寫截圖方法;本文的截圖方法適用wkwebview截長圖,但是如果網(wǎng)頁有導(dǎo)航,可能會存在問題,即多個導(dǎo)航拼接的情況。這種可能考慮另一種方案了。
#import <WebKit/WebKit.h>
#import <UIKit/UIKit.h>
@interface WKWebView (CYScreenshotWebView)
- (NSArray *)prepareSnapShotOffsets;
- (void)captureWebSnapshotWithScale:(CGFloat)scale andCompletion:(void (^)(UIImage *capturedImage)) completion;
@end
#import "WKWebView+CYScreenshotWebView.h"
@implementation WKWebView (CYScreenshotWebView)
//根據(jù)當(dāng)前網(wǎng)頁的內(nèi)容大?。淳W(wǎng)頁的scrollView的contentSize)來創(chuàng)建一個圖形上下文,保存每次渲染的網(wǎng)頁內(nèi)容
- (void)captureWebSnapshotWithScale:(CGFloat)scale andCompletion:(void (^)(UIImage *capturedImage))completion{
? ? //為達到無感知截圖,添加遮照,截圖完成后去掉
? ? UIView *coverView = [self snapshotViewAfterScreenUpdates:YES];
? ? coverView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, coverView.frame.size.width, coverView.frame.size.height);
? ? [self.superview addSubview:coverView];
? ? //首先根據(jù)網(wǎng)頁內(nèi)容大?。╟ontentSize)與顯示大?。╞ounds.size)的關(guān)系,計算網(wǎng)頁需要截取多少次圖,以及每次截圖的頁面的偏移量contentOffset
? ? NSArray *offsets = [self prepareSnapShotOffsets];
? ? CGPoint bckUp = self.scrollView.contentOffset;//保存網(wǎng)頁當(dāng)前偏移量。便于截圖
? ? UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, YES, scale);
? ? //根據(jù)步驟2中所獲取的偏移點的坐標(biāo),將當(dāng)前顯示的內(nèi)容渲染到圖形上下文中,然后繼續(xù)滾動和渲染直到將所有網(wǎng)頁內(nèi)容渲染完成,結(jié)束上下文
? ? __weak __typeof(self) weakSelf = self;
? ? [self captureAtPage:0 point:offsets drawCallback:^{
? ? ? ? UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
? ? ? ? UIGraphicsEndImageContext();
? ? ? ? if (completion) {
? ? ? ? ? ? completion(captureImage);
? ? ? ? }
? ? ? ? __strong __typeof(weakSelf) strongSelf = weakSelf;
? ? ? ? [strongSelf.scrollView setContentOffset:bckUp animated:false];//恢復(fù)截圖前
? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? ? ? [coverView removeFromSuperview];//去掉遮罩
? ? ? ? });
? ? }];
}
- (NSArray *)prepareSnapShotOffsets{
? ? NSMutableArray *offsets = [NSMutableArray arrayWithCapacity:0];
? ? int pageX = ceilf(self.scrollView.contentSize.width / self.scrollView.bounds.size.width);
? ? int pageY = ceilf(self.scrollView.contentSize.height / self.scrollView.bounds.size.height);
? ? for (int i = 0; i< pageX *pageY; i++) {
? ? ? ? int x = i / pageY;
? ? ? ? int y = i % pageY;
? ? ? ? CGFloat orginX = x * self.scrollView.bounds.size.width;
? ? ? ? CGFloat orginY = y *self.scrollView.bounds.size.height;
? ? ? ? NSValue *value = [NSValue valueWithCGPoint:CGPointMake(orginX, orginY)];
? ? ? ? [offsets addObject:value];
? ? }
? ? return offsets;
}
-(void)captureAtPage:(int)page point:(NSArray *)offsets drawCallback:(void(^)(void))callback {
? ? NSValue *valuePoint = offsets[page];
? ? CGPoint point = [valuePoint CGPointValue];
? ? [self.scrollView setContentOffset:point animated:NO];
? ? __weak __typeof(self)weakSelf = self;
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? __strong __typeof(weakSelf)strongSelf = weakSelf;
? ? ? ? CGRect rect = CGRectMake(point.x, point.y, strongSelf.scrollView.bounds.size.width, strongSelf.scrollView.bounds.size.height);
? ? ? ? BOOL finish = [strongSelf.scrollView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
? ? ? ? if (finish) {
? ? ? ? ? ? if (page < offsets.count - 1) {
? ? ? ? ? ? ? ? [strongSelf captureAtPage:page + 1 point:offsets drawCallback:callback];
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? callback();
? ? ? ? ? ? }
? ? ? ? }
? ? });
}
//保存圖片到內(nèi)存
- (void)saveShareImageInTmp{
??? NSData *data = UIImagePNGRepresentation(_currentImg);
??? //截圖存入文件夾
??? _imgFilePath = [self tempFilePath:@"shareImg.png"];
??? [data writeToFile:_imgFilePath atomically:YES];
}
//保存圖片到相冊
- (void)saveImgToLibrary{
? ? [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
? ? ? ? PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:self.currentImg];
? ? } completionHandler:^(BOOL success, NSError * _Nullable error) {
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? self.saveImgBtn.enabled = YES;
? ? ? ? ? ? [self.view CY_showHudText:@"保存完成" duration:2.0];
? ? ? ? });
? ? }];
}
#pragma mark - 分享方法
//分享鏈接
- (void)shareClick:(UIButton *)sender{
? ? _docVC = [[UIDocumentInteractionController alloc] init];
? ? _docVC.URL = [NSURL URLWithString:_shareUrl];
? ? [_docVC presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
}
//1、截網(wǎng)頁整張圖并分享
- (void)setupQRCodeImageWithCatoryWebViewAndShare{
? ? __weak __typeof(self) weakSelf = self;
? [self.cotentWebView captureWebSnapshotWithScale: [UIScreen
mainScreen].nativeScale andCompletion:^(UIImage * _Nonnull
capturedImage) {
? ? ? ? weakSelf.currentImg? = capturedImage;
? ? ? ? //保存到臨時文件下
? ? ? ? [weakSelf saveShareImageInTmp];
? ? ? ? weakSelf.docVC = [[UIDocumentInteractionController alloc] init];
? ? ? ? weakSelf.docVC.URL = [NSURL fileURLWithPath:weakSelf.imgFilePath];
? ? ? ? [weakSelf.docVC presentOptionsMenuFromRect:weakSelf.view.frame inView:weakSelf.view animated:YES];
? ? ? ? weakSelf.inviteBtn.enabled = YES;
? ? }];
}
//2.截網(wǎng)頁整張圖并保存相冊
- (void)setupQRCodeImageWithCatoryWebViewAndSave{
??? __weak __typeof(self) weakSelf = self;
??? [self.cotentWebView captureWebSnapshotWithScale:[UIScreen mainScreen].nativeScale andCompletion:^(UIImage * _Nonnull capturedImage) {
??????? weakSelf.currentImg? = capturedImage;
??????? [self saveImgToLibrary];
??? }];
}