- (UIImage *)cutPic{
CGRect snapshotFrame = CGRectMake(0, 0, _webView.scrollView.contentSize.width, _webView.scrollView.contentSize.height);
UIEdgeInsets snapshotEdgeInsets = UIEdgeInsetsZero;
UIImage *shareImage = [self snapshotViewFromRect:snapshotFrame withCapInsets:snapshotEdgeInsets];
NSString *path_document = NSHomeDirectory();
//设置一个图片的存储路径
NSString *imagePath = [path_document stringByAppendingString:@"/Documents/picc.png"];
//把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取)
[UIImagePNGRepresentation(shareImage) writeToFile:imagePath atomically:YES];
return shareImage;
}
或者保存到相册:
UIImageWriteToSavedPhotosAlbum(shareImage, self, @selector(completedWithImage:error:context:), NULL);
- (void)completedWithImage:(UIImage *)image error:(NSError *)error context:(void *)contextInfo{
NSString *toast = (!image || error)? [NSString stringWithFormat:@"保存图片失败 , 错误:%@",error] : @"保存图片成功";
[self.view makeToast:toast duration:2.0 position:CSToastPositionCenter];
}
***
// 网页长截图
- (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {
CGFloat scale = [UIScreen mainScreen].scale;
CGSize boundsSize = self.webView.bounds.size;
CGFloat boundsWidth = boundsSize.width;
CGFloat boundsHeight = boundsSize.height;
CGSize contentSize = self.webView.scrollView.contentSize;
CGFloat contentHeight = contentSize.height;
// CGFloat contentWidth = contentSize.width;
CGPoint offset = self.webView.scrollView.contentOffset;
[self.webView.scrollView setContentOffset:CGPointMake(0, 0)];
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, [UIScreen mainScreen].scale);
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = self.webView.scrollView.contentOffset.y;
[self.webView.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[self.webView.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();
UIGraphicsEndImageContext();
UIImageView * snapshotView = [[UIImageView alloc]initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
snapshotView.image = [fullImage resizableImageWithCapInsets:capInsets];
return snapshotView.image;
}
iOS UIWebView网页长截图
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- UIWebView十分适合冬日里让手机给取暖 创建UIWebView 获取页面的名称 webtitle = [se...
- 前几天写项目的时候, 后台给返回的数据是 html 类型的一篇文章, 我将其通过 [webView loadHTM...