iOS 网页截屏

2019-03-21  本文已影响0人  Devin_明

#pragma mark--截屏处理

//截屏处理

- (UIImage*)cutPic{

 CGRectsnapshotFrame = CGRectMake(0, 0, _webView.scrollView.contentSize.width, _webView.scrollView.contentSize.height);

UIEdgeInsetssnapshotEdgeInsets = UIEdgeInsetsZero;

 UIImage*shareImage = [selfsnapshotViewFromRect:snapshotFrame withCapInsets:snapshotEdgeInsets];

 returnshareImage;

}

// 网页长截图

- (UIImage*)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {

 CGFloatscale = [UIScreenmainScreen].scale;

 CGSizeboundsSize = self.webView.bounds.size;

 CGFloatboundsWidth = boundsSize.width;

 CGFloatboundsHeight = boundsSize.height;

 CGSizecontentSize = self.webView.scrollView.contentSize;

 CGFloatcontentHeight = contentSize.height;

 //    CGFloat contentWidth = contentSize.width;

 CGPointoffset = self.webView.scrollView.contentOffset;

    [self.webView.scrollViewsetContentOffset:CGPointMake(0, 0)];

 NSMutableArray*images = [NSMutableArrayarray];

 while(contentHeight > 0) {

 UIGraphicsBeginImageContextWithOptions(boundsSize, NO, [UIScreenmainScreen].scale);

        [self.webView.layerrenderInContext:UIGraphicsGetCurrentContext()];

 UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();

[images addObject:image];

 CGFloatoffsetY = self.webView.scrollView.contentOffset.y;

        [self.webView.scrollViewsetContentOffset:CGPointMake(0, offsetY + boundsHeight)];

        contentHeight -= boundsHeight;

    }

    [self.webView.scrollViewsetContentOffset:offset];

 CGSizeimageSize = CGSizeMake(contentSize.width* scale,

                                  contentSize.height* scale);

 UIGraphicsBeginImageContext(imageSize);

[images enumerateObjectsUsingBlock:^(UIImage*image, NSUIntegeridx, BOOL*stop) {

[image drawInRect:CGRectMake(0,

                                     scale * boundsHeight * idx,

                                     scale * boundsWidth,

                                     scale * boundsHeight)];

    }];

 UIImage*fullImage = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();

 UIImageView* snapshotView = [[UIImageViewalloc]initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];

    snapshotView.image= [fullImage resizableImageWithCapInsets:capInsets];

 NSData*data = UIImageJPEGRepresentation(snapshotView.image, 0.3);

 return[UIImageimageWithData:data];

}

上一篇下一篇

猜你喜欢

热点阅读