iOS-WKWebView UIScrollView 全屏截圖,網頁全屏截圖 UiTableView UIScrollView 全屏截圖

iOS-WKWebView UIScrollView 全屏截圖,網頁全屏截圖
UiTableView? UIScrollView 全屏截圖

文章來自于 :https://www.jishudog.com/5698/html

借鑒:http://www.itdecent.cn/p/22b0f792ad18

UITableView+SnapImage.h

- (void)ZFJContentCaptureCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler;

- (void)ZFJContentCaptureWithoutOffsetCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler;

UITableView+SnapImage.m

- (void)ZFJContentCaptureCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler{

? ? CGPoint offset = self.contentOffset;


? ? UIView *snapShotView = [self snapshotViewAfterScreenUpdates:YES];

? ? snapShotView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, snapShotView.frame.size.width, snapShotView.frame.size.height);

? ? [self.superview addSubview:snapShotView];


? ? if(self.frame.size.height < self.contentSize.height){

? ? ? ? self.contentOffset = CGPointMake(0, self.contentSize.height - self.frame.size.height);

? ? }


? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

? ? ? ? self.contentOffset = CGPointZero;


? ? ? ? [self ZFJContentCaptureWithoutOffsetCompletionHandler:^(UIImage *capturedImage) {

? ? ? ? ? ? self.contentOffset = offset;


? ? ? ? ? ? [snapShotView removeFromSuperview];


? ? ? ? ? ? completionHandler(capturedImage);

? ? ? ? }];

? ? });


}

- (void)ZFJContentCaptureWithoutOffsetCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler{

? ? UIView *containerView = [[UIView alloc]initWithFrame:self.bounds];


? ? CGRect bakFrame = self.frame;

? ? UIView *bakSuperView = self.superview;

? ? NSInteger bakIndex = [self.superview.subviews indexOfObject:self];


? ? [self removeFromSuperview];

? ? [containerView addSubview:self];


? ? CGSize totalSize = self.contentSize;


? ? float page = floorf(totalSize.height/containerView.bounds.size.height);


? ? self.frame = CGRectMake(0, 0, containerView.bounds.size.width, self.contentSize.height);

? ? UIGraphicsBeginImageContextWithOptions(totalSize, false, [UIScreen mainScreen].scale);

? ? [self ZFJContentPageDrawTargetView:containerView index:0 maxIndex:(int)page drawCallback:^{

? ? ? ? UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();

? ? ? ? UIGraphicsEndImageContext();


? ? ? ? [self removeFromSuperview];

? ? ? ? [bakSuperView insertSubview:self atIndex:bakIndex];


? ? ? ? self.frame = bakFrame;


? ? ? ? [containerView removeFromSuperview];


? ? ? ? completionHandler(capturedImage);

? ? }];


}

- (void)ZFJContentPageDrawTargetView:(UIView *)targetView index:(int)index maxIndex:(int)maxIndex drawCallback:(void(^)(void))drawCallback{

? ? CGRect splitFrame = CGRectMake(0, (float)index * targetView.frame.size.height, targetView.bounds.size.width, targetView.frame.size.height);


? ? CGRect myFrame = self.frame;

? ? myFrame.origin.y = - ((float)index * targetView.frame.size.height);

? ? self.frame = myFrame;


? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

? ? ? ? [targetView drawViewHierarchyInRect:splitFrame afterScreenUpdates:YES];


? ? ? ? if(index<maxIndex){

? ? ? ? ? ? [self ZFJContentPageDrawTargetView:targetView index:index + 1 maxIndex:maxIndex drawCallback:drawCallback];

? ? ? ? }else{

? ? ? ? ? ? drawCallback();

? ? ? ? }

? ? });

}

WKWebView+SnapImage.m

- (void)ZFJContentCaptureCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler{

? ? CGPoint offset = self.scrollView.contentOffset;


? ? UIView *snapShotView = [self snapshotViewAfterScreenUpdates:YES];

? ? snapShotView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, snapShotView.frame.size.width, snapShotView.frame.size.height);

? ? [self.superview addSubview:snapShotView];


? ? if(self.frame.size.height < self.scrollView.contentSize.height){

? ? ? ? self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentSize.height - self.frame.size.height);

? ? }


? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

? ? ? ? self.scrollView.contentOffset = CGPointZero;


? ? ? ? [self ZFJContentCaptureWithoutOffsetCompletionHandler:^(UIImage *capturedImage) {

? ? ? ? ? ? self.scrollView.contentOffset = offset;


? ? ? ? ? ? [snapShotView removeFromSuperview];


? ? ? ? ? ? completionHandler(capturedImage);

? ? ? ? }];

? ? });


}

- (void)ZFJContentCaptureWithoutOffsetCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler{

? ? UIView *containerView = [[UIView alloc]initWithFrame:self.bounds];


? ? CGRect bakFrame = self.frame;

? ? UIView *bakSuperView = self.superview;

? ? NSInteger bakIndex = [self.superview.subviews indexOfObject:self];


? ? [self removeFromSuperview];

? ? [containerView addSubview:self];


? ? CGSize totalSize = self.scrollView.contentSize;


? ? float page = floorf(totalSize.height/containerView.bounds.size.height);


? ? self.frame = CGRectMake(0, 0, containerView.bounds.size.width, self.scrollView.contentSize.height);

? ? UIGraphicsBeginImageContextWithOptions(totalSize, false, [UIScreen mainScreen].scale);

? ? [self ZFJContentPageDrawTargetView:containerView index:0 maxIndex:(int)page drawCallback:^{

? ? ? ? UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();

? ? ? ? UIGraphicsEndImageContext();


? ? ? ? [self removeFromSuperview];

? ? ? ? [bakSuperView insertSubview:self atIndex:bakIndex];


? ? ? ? self.frame = bakFrame;


? ? ? ? [containerView removeFromSuperview];


? ? ? ? completionHandler(capturedImage);

? ? }];


}

- (void)ZFJContentPageDrawTargetView:(UIView *)targetView index:(int)index maxIndex:(int)maxIndex drawCallback:(void(^)(void))drawCallback{

? ? CGRect splitFrame = CGRectMake(0, (float)index * targetView.frame.size.height, targetView.bounds.size.width, targetView.frame.size.height);


? ? CGRect myFrame = self.frame;

? ? myFrame.origin.y = - ((float)index * targetView.frame.size.height);

? ? self.frame = myFrame;


? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

? ? ? ? [targetView drawViewHierarchyInRect:splitFrame afterScreenUpdates:YES];


? ? ? ? if(index<maxIndex){

? ? ? ? ? ? [self ZFJContentPageDrawTargetView:targetView index:index + 1 maxIndex:maxIndex drawCallback:drawCallback];

? ? ? ? }else{

? ? ? ? ? ? drawCallback();

? ? ? ? }

? ? });

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容