iOS截长图

WKWebView截图、View生成图片

920 发布: 2020/7/24 11:21 本文总阅读量

WKWebView截图

/** webView截屏 deviation:底部偏移量*/
+(UIImage *)screenshotWhitWK:(WKWebView *)wkweb andDeviation:(CGFloat)deviation{
    
    CGFloat boundsWidth = wkweb.bounds.size.width;
    CGFloat boundsHeight = wkweb.bounds.size.height;
    
    UIScrollView *scrollView = wkweb.scrollView;
    CGPoint oldOffset = scrollView.contentOffset;
    [scrollView setContentOffset:CGPointZero animated:NO];
    
    CGFloat contentHeight = scrollView.contentSize.height-deviation;
    CGSize shotSize = CGSizeMake(scrollView.contentSize.width, contentHeight);
    CGFloat scale = [UIScreen mainScreen].scale;
    NSMutableArray *images = [NSMutableArray array];
    while (contentHeight > 0) {UIGraphicsBeginImageContextWithOptions(wkweb.bounds.size, NO, scale);
        [wkweb.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [images addObject:image];
        CGFloat offsetY = scrollView.contentOffset.y;
        [scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight) animated:NO];
        contentHeight -= boundsHeight;
    }
    [scrollView setContentOffset:oldOffset animated:NO];UIGraphicsBeginImageContextWithOptions(shotSize, NO, scale);
    for (NSInteger index = 0; index < images.count; index++) {
        UIImage *image = images[index];
        [image drawInRect:CGRectMake(0, boundsHeight * index, boundsWidth, boundsHeight)];
    }
    UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return fullImage;
}

####View生成图片

#####1.将传入的View生成图片:

+ (UIImage *)makeImageWithView:(UIView *)view{
    CGSize s = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];   
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext(); 
    return image;
}

#####2.将传入的View生成图片,可以显示layer(分享分时图、k线)

+ (UIImage *)imageFromView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);  
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

#####3.将tableview生成图片

+ (UIImage *)getTableViewimage:(UITableView *)tableview{
    UIImage* viewImage = nil;
    UIScrollView *scrollView = tableview;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(scrollView.contentSize.width, scrollView.contentSize.height), NO, 0.0);
    {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;

        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        viewImage = UIGraphicsGetImageFromCurrentImageContext();

        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    if (viewImage != nil) {
        return viewImage;
    }
    return nil;
}

#####4.图片拼接

+ (UIImage *)addSlaveImage:(UIImage *)slaveImage toMasterImage:(UIImage *)masterImage {
    CGSize size;
    size.width = masterImage.size.width;
    size.height = masterImage.size.height + slaveImage.size.height;  
    UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);  
    [masterImage drawInRect:CGRectMake(0, 0, masterImage.size.width, masterImage.size.height)];
    [slaveImage drawInRect:CGRectMake(0, masterImage.size.height, masterImage.size.width, slaveImage.size.height)];   
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();
    return resultImage;
}