我们在开发中经常需要使用截屏功能或者把某个view生成一张图片的功能,还有可能需要拼接在一起组成一张大图,另外有可能给一张大图添加水印图等。
屏幕截图
屏幕截图和view截成图一样,只是把对应的view换成window就行,示例代码如下
UIGraphicsBeginImageContextWithOptions([UIApplication sharedApplication].keyWindow.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIApplication sharedApplication].keyWindow .layer renderInContext:context];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这样截图出来的图片没有顶部状态栏,因为状态栏并不在keyWindow对应的view上,所以我们要加上相应的UIStatueBar
UIGraphicsBeginImageContextWithOptions([UIApplication sharedApplication].keyWindow.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIApplication sharedApplication].keyWindow .layer renderInContext:context];
//添加顶部状态栏
UIView *statusBarView = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
[statusBarView.layer renderInContext:context];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
获取顶部状态栏View的方法OTScreenshotHelper中用的是runtime,也是可以获取到,有兴趣的同学可以去看下https://github.com/OpenFibers/OTScreenshotHelper
以上方法还有一个问题,就是对于有UIPickerView在页面上,PickView会有黑边,所以我们要把renderInContext替换成drawViewHierarchyInRect,示例代码如下
UIGraphicsBeginImageContextWithOptions([UIApplication sharedApplication].keyWindow.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIApplication sharedApplication].keyWindow drawViewHierarchyInRect:(CGRect){.origin=CGPointZero, .size=[UIApplication sharedApplication].keyWindow.bounds.size} afterScreenUpdates:YES];
//添加顶部状态栏
UIView *statusBarView = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
[statusBarView.layer renderInContext:context];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIScrollView图片裁剪
一般的view的生成图片和上面截屏方法类似,而且可以不考虑顶部状态栏。对于UIScrollView用上面的方法获取的图片也是我们可见的,如果我们想要获取整个ContentSize的图片,对于UITableView和UICollectionView往往会留下一片空白部分,达不到我们想要的效果。要达到整个都截取的话,我们可以分段截图,然后拼接,也可以改变其frame,让所有cell都渲染出来。
- (UIImage *)screenshotImageWithScrollView:(UIScrollView *)scrollView
imageSize:(CGSize)size{
CGPoint orignalContentOffset = scrollView.contentOffset;
CGRect orignalFrame = scrollView.frame;
UIScrollView *rt = scrollView;
CGRect frm = scrollView.frame;
frm.size.height = size.height;
rt.frame = frm;
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[rt.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
scrollView.contentOffset = orignalContentOffset;
scrollView.frame = orignalFrame;
return viewImage;
}
上面就是改变ScrollView的frame,高度和contentSize的height一样,等截图才做完成后重新回到它原状态。裁剪ScrollView生成图片的尺寸和我们给的尺寸有变差,需要注意生成图片的scale,裁剪示例代码如下
UIImage *screenImg = [self screenshotImageWithScrollView:scrollView imageSize:scrollView.contentSize];
CGImageRef screenImageRef = screenImg.CGImage;
CGImageRef rectImageRef = CGImageCreateWithImageInRect(screenImageRef, CGRectMake(rect.origin.x*screenImg.scale, rect.origin.y*screenImg.scale, rect.size.width*screenImg.scale, rect.size.height*screenImg.scale));
UIImage* rectImage = [UIImage imageWithCGImage:rectImageRef];
CGImageRelease(rectImageRef);
图片添加水印
图片拼接、添加水印都基本一样,都在画布中画出图片
- (UIImage *)imageAddMaskWithImage:(UIImage *)orignalImage
imageSize:(CGSize)imageSize
maskImage:(UIImage *)maskImage
maskImageRect:(CGRect)maskRect
maskAlpha:(CGFloat)maskAlpha{
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[orignalImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[maskImage drawInRect:maskRect blendMode:kCGBlendModeNormal alpha:maskAlpha];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
总结
以上都是静态页面的截图,对于正在执行动画的页面没测试,有兴趣的同学可以尝试一下。另外对于WebView上述方式是截取不了整个WebView,这有个解决方法//www.greatytc.com/p/ef50defb979d,最后附上demoOC、Swift。