国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中如何獲取某個(gè)視圖的截圖詳析

2020-07-26 02:19:33
字體:
供稿:網(wǎng)友

前言

最近在做SDK的截圖,想觸發(fā)類似系統(tǒng)的截屏功能,找了一圈,總結(jié)一下靠譜的幾種方式。

我寫了個(gè)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渲染的部分,這時(shí)候就要用到改進(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) 添加到某個(gè)View 上后,使用上面的幾種方式都無法截取到Layer上的內(nèi)容,這個(gè)時(shí)候可以使用系統(tǒng)的一個(gè)API,但是該API只能返回一個(gè)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)牙打印的時(shí)候,嘗試過將UIWebView 的內(nèi)容轉(zhuǎn)換為UIImage,寫過一個(gè)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í)價(jià)值,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 当阳市| 许昌县| 沂水县| 静乐县| 大兴区| 鲁甸县| 宝兴县| 石首市| 美姑县| 镇雄县| 叶城县| 罗城| 宁强县| 读书| 景洪市| 平湖市| 衢州市| 云梦县| 白城市| 鸡西市| 娄烦县| 苍南县| 阳朔县| 武城县| 沙坪坝区| 沭阳县| 永新县| 肇庆市| 肇源县| 将乐县| 永泰县| 武隆县| 长丰县| 丹江口市| 沂源县| 建德市| 福贡县| 武乡县| 娄底市| 西和县| 偃师市|