iOS 中獲取某個檢視的截圖
阿新 • • 發佈:2018-11-17
最近在做SDK的截圖,想觸發類似系統的截圖功能,找了一圈,總結一下靠譜的幾種方式。
我寫了個UIView 的category,將這幾種方式封裝和簡化了一下。
第一種情形截圖
這種是最最普通的截圖,針對一般的檢視上新增檢視的情況,基本都可以使用。
原始碼:
/**
普通的截圖
該API僅可以在未使用layer和OpenGL渲染的檢視上使用
@return 擷取的圖片
*/
- (UIImage *)nomalSnapshotImage
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
第二種情形截圖
如果一些檢視是用OpenGL渲染出來的,那麼使用上面的方式就無法截圖到OpenGL渲染的部分,這時候就要用到改進後的截圖方案:
/**
針對有用過OpenGL渲染過的檢視截圖
@return 擷取的圖片
*/
- (UIImage *)openglSnapshotImage
{
CGSize size = self.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGRect rect = self.frame;
[self drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
第三種情形截圖
有一些特殊的Layer(比如:AVCaptureVideoPreviewLayer
和 AVSampleBufferDisplayLayer
) 新增到某個View 上後,使用上面的幾種方式都無法擷取到Layer上的內容,這個時候可以使用系統的一個API,但是該API只能返回一個UIView,返回的UIView 可以修改frame 等引數。
/**
截圖
以UIView 的形式返回(_UIReplicantView)
@return 截取出來的圖片轉換的檢視
*/
- (UIView *)snapshotView
{
UIView *snapView = [self snapshotViewAfterScreenUpdates:YES];
return snapView;
}
遺留問題:
通過方式三擷取的UIView,無法轉換為UIImage,我試過將返回的截圖View寫入點陣圖再轉換成UIImage,但是返回的UIImage 要麼為空,要麼沒有內容。如果有人知道解決方案請告知我。
UIWebView的截圖
去年在做藍芽列印的時候,嘗試過將UIWebView 的內容轉換為UIImage,寫過一個UIWebView的category,也算是對UIWebView 的截圖,順便也貼出來吧
- (UIImage *)imageForWebView
{
// 1.獲取WebView的寬高
CGSize boundsSize = self.bounds.size;
CGFloat boundsWidth = boundsSize.width;
CGFloat boundsHeight = boundsSize.height;
// 2.獲取contentSize
CGSize contentSize = self.scrollView.contentSize;
CGFloat contentHeight = contentSize.height;
// 3.儲存原始偏移量,便於截圖後復位
CGPoint offset = self.scrollView.contentOffset;
// 4.設定最初的偏移量為(0,0);
[self.scrollView setContentOffset:CGPointMake(0, 0)];
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
// 5.獲取CGContext 5.獲取CGContext
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 6.渲染要擷取的區域
[self.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 7.擷取的圖片儲存起來
[images addObject:image];
CGFloat offsetY = self.scrollView.contentOffset.y;
[self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
// 8 webView 恢復到之前的顯示區域
[self.scrollView setContentOffset:offset];
CGFloat scale = [UIScreen mainScreen].scale;
CGSize imageSize = CGSizeMake(contentSize.width * scale,
contentSize.height * scale);
// 9.根據裝置的解析度重新繪製、拼接成完整清晰圖片
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();
return fullImage;
}