溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS中怎么獲取某個視圖的截圖

發布時間:2021-09-26 16:19:46 來源:億速云 閱讀:191 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關iOS中怎么獲取某個視圖的截圖的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

第一種情形截圖

這種是最最普通的截圖,針對一般的視圖上添加視圖的情況,基本都可以使用。

源碼:

/** 普通的截圖 該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;}

感謝各位的閱讀!關于“iOS中怎么獲取某個視圖的截圖”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

ios
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女