iOS中如何獲取某個視圖的截圖詳析
前言
最近在做SDK的截圖,想觸發(fā)類似系統(tǒng)的截屏功能,找了一圈,總結(jié)一下靠譜的幾種方式。
我寫了個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渲染的部分,這時候就要用到改進(jìn)后的截圖方案:
/** 針對有用過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上的內(nèi)容,這個時候可以使用系統(tǒng)的一個API,但是該API只能返回一個UIView,返回的UIView 可以修改frame 等參數(shù)。
/** 截圖 以UIView 的形式返回(_UIReplicantView) @return 截取出來的圖片轉(zhuǎn)換的視圖 */ - (UIView *)snapshotView { UIView *snapView = [self snapshotViewAfterScreenUpdates:YES]; return snapView; }
遺留問題:
通過方式三截取的UIView,無法轉(zhuǎn)換為UIImage,我試過將返回的截圖View寫入位圖再轉(zhuǎn)換成UIImage,但是返回的UIImage 要么為空,要么沒有內(nèi)容。如果有人知道解決方案請告知我。
UIWebView的截圖
去年在做藍(lán)牙打印的時候,嘗試過將UIWebView 的內(nèi)容轉(zhuǎn)換為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.保存原始偏移量,便于截圖后復(fù)位 CGPoint offset = self.scrollView.contentOffset; // 4.設(shè)置最初的偏移量為(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.渲染要截取的區(qū)域 [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 恢復(fù)到之前的顯示區(qū)域 [self.scrollView setContentOffset:offset]; CGFloat scale = [UIScreen mainScreen].scale; CGSize imageSize = CGSizeMake(contentSize.width * scale, contentSize.height * scale); // 9.根據(jù)設(shè)備的分辨率重新繪制、拼接成完整清晰圖片 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; }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線
本篇文章主要介紹了iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線,非常具有實用價值,需要的朋友可以參考下2017-05-05針對iOS開發(fā)的一些Xcode使用技巧小結(jié)
這篇文章主要介紹了針對iOS開發(fā)的一些Xcode使用技巧小結(jié),Xcode是Mac上編寫iOS應(yīng)用的開發(fā)環(huán)境,需要的朋友可以參考下2015-12-12iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法
本篇文章主要介紹了iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01