普通view截图
1 2 3 4 5 6 7 8 9 10 11
| + (UIImage *)gjj_viewScreenShot:(UIView *)view { UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height ), NO, 0.0) [view.layer renderInContext:UIGraphicsGetCurrentContext()] /// 返回一个基于当前图形上下文的图片 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext()
/// 移除栈顶的基于当前位图的图形上下文 UIGraphicsEndImageContext() return viewImage }
|
实现步骤
- 保存scrollView截取前的偏移量及Frame
- 计算出你要截取的长图的高度及宽度,即scrollView的contentSize,将scrollView.frame设成scrollView.contentSize
- 渲染出scrollView整体(上下文),截取当前scrollView生成Image
- 恢复scrollView的偏移量及Frame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| + (UIImage *)gjj_scrollViewScreenShot:(UIScrollView *)scrollView { UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, [UIScreen mainScreen].scale); CGPoint saveContentOffset = scrollView.contentOffset; CGRect savedFrame = scrollView.frame; scrollView.contentOffset = CGPointZero; scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height); [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; scrollView.layer.contents = nil; image = UIGraphicsGetImageFromCurrentImageContext(); scrollView.contentOffset = saveContentOffset; scrollView.frame = savedFrame; UIGraphicsEndImageContext(); return image; }
|
webView生成长截图
webView和scrollView的截图大同小异
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| + (UIImage *)gjj_webViewScreenShot:(UIWebView *)webView { UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, YES, [UIScreen mainScreen].scale); CGPoint saveContentOffset = webView.scrollView.contentOffset; CGRect savedFrame = webView.frame; webView.scrollView.contentOffset = CGPointZero; webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height); [webView.layer renderInContext:UIGraphicsGetCurrentContext()]; webView.layer.contents = nil; image = UIGraphicsGetImageFromCurrentImageContext(); webView.scrollView.contentOffset = saveContentOffset; webView.frame = savedFrame; UIGraphicsEndImageContext(); return image; }
|
图片拼接
很多需求是截图附带二维码等进行分享。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| + (UIImage *)gjj_addHeadImage:(UIImage *)headImage footImage:(UIImage *)footImage toMasterImage:(UIImage *)masterImage { CGSize size size.width = masterImage.size.width
CGFloat headProportion = !headImage?0:(headImage.size.width / headImage.size.height) CGFloat footProportion = !footImage?0:(footImage.size.width / footImage.size.height)
CGFloat headHeight = !headImage? 0:masterImage.size.width/headProportion CGFloat footHeight = !footImage? 0:footImage.size.width/footProportion
size.height = masterImage.size.height + headHeight + footHeight
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0)
if (headImage) { [headImage drawInRect:CGRectMake(0, 0, masterImage.size.width, headHeight)] } [masterImage drawInRect:CGRectMake(0, headHeight, masterImage.size.width, masterImage.size.height)] if (footImage) { [footImage drawInRect:CGRectMake(0, masterImage.size.height + headHeight, masterImage.size.width, footHeight)] } UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext() return resultImage }
|
添加水印
1 2 3 4 5 6 7 8 9 10
| + (UIImage *)gjj_addWaterImage:(UIImage *)waterImage toMasterImage:(UIImage *)masterImage waterImageRect:(CGRect)rect { UIGraphicsBeginImageContextWithOptions(masterImage.size, NO, 0) [masterImage drawInRect:CGRectMake(0, 0, masterImage.size.width, masterImage.size.height)] /// 绘制水印图片到当前上下文 [waterImage drawInRect:rect]
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage }
|