iOS开发小技巧

iOS WKWebView内容转图记录

2019-06-09  本文已影响0人  整个夏天

遇到的问题

解决思路

由于以上三个问题都是逐次出现,解决不是一次性绘制完成则需要从ScrollView的第一页滚动开始截图,而如果滚动的速度太快,用户是无法察觉的,但是有的地方生成的图片是空白的,如果延时截图则会让用户看到视图的滚动。
为了让用户看不到滚动的效果我们需要在滚动截图开始前生成一张内容图片覆盖在WKWebView上,让用户察觉不到视图的滚动,然后开始滚动一张一张的截图,最后把这一张一张的截图拼接起来,然后移除覆盖在WKWebView上的图片即可。

生成图片参考文章

//
//  WKWebView+FullImg.h
//  word
//
//  Created by stonemover on 2019/6/8.
//  Copyright © 2019 stonemover. All rights reserved.
//

#import <WebKit/WebKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface WKWebView (FullImg)

- (void)imageRepresentation:(void(^)(UIImage * img))block;

@end

NS_ASSUME_NONNULL_END

//
//  WKWebView+FullImg.m
//  word
//
//  Created by stonemover on 2019/6/8.
//  Copyright © 2019 stonemover. All rights reserved.
//

#import "WKWebView+FullImg.h"
#import "AppDelegate.h"

@implementation WKWebView (FullImg)

- (void)imageRepresentation:(void(^)(UIImage * img))block{
    CGFloat scale = [UIScreen mainScreen].scale;
    
    CGSize boundsSize = self.bounds.size;
    CGFloat boundsWidth = boundsSize.width;
    CGFloat boundsHeight = boundsSize.height;
    
    CGSize contentSize = self.scrollView.contentSize;
    CGFloat contentHeight = contentSize.height;
    
    CGPoint offset = self.scrollView.contentOffset;
    
    NSMutableArray *images = [NSMutableArray array];
    //创建一个view覆盖在上面
    [self createPlaceholder:images];
    
    
    [self.scrollView setContentOffset:CGPointMake(0, 0)];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self createImg:images height:contentHeight block:^{
            [self.scrollView setContentOffset:offset];
            
            CGSize imageSize = CGSizeMake(contentSize.width * scale,
                                          contentSize.height * scale);
            UIGraphicsBeginImageContext(imageSize);
            [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
                [image drawInRect:CGRectMake(0,
                                             scale * boundsHeight * idx,
                                             scale * boundsWidth,
                                             scale * boundsHeight)];
            }];
            UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
            //        UIScrollView * scrollView =[[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            //        UIImageView * imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)];
            //        imgView.image=fullImage;
            //        [scrollView addSubview:imgView];
            //        [scrollView setContentSize:contentSize];
            //        scrollView.pagingEnabled=YES;
            //        [APPDELEGATE.window addSubview:scrollView];
            UIGraphicsEndImageContext();
            block(fullImage);
        }];
    });
    
    
}

- (void)createImg:(NSMutableArray*)images height:(CGFloat)contentHeight block:(void(^)(void))block{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [images addObject:image];
    CGFloat offsetY = self.scrollView.contentOffset.y;
    [self.scrollView setContentOffset:CGPointMake(0, offsetY + self.bounds.size.height)];
    contentHeight -= self.bounds.size.height;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (contentHeight>0) {
            [self createImg:images height:contentHeight block:block];
        }else{
            for (UIView * v in self.superview.subviews) {
                if ([v isKindOfClass:[UIImageView class]]) {
                    UIImageView * imgChild=(UIImageView*)v;
                    if (imgChild.image==images[0]) {
                        [imgChild removeFromSuperview];
                        [images removeObjectAtIndex:0];
                        break;
                    }
                }
            }
            block();
        }
    });
}

- (void)createPlaceholder:(NSMutableArray*)imgs{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView * imgViewPlaceholder=[[UIImageView alloc] initWithImage:image];
    imgViewPlaceholder.tag=100;
    imgViewPlaceholder.frame=self.frame;
    [self.superview addSubview:imgViewPlaceholder];
    [imgs addObject:image];
}

@end

上一篇 下一篇

猜你喜欢

热点阅读