iOS中如何獲取某個(gè)視圖的截圖詳析
前言
最近在做SDK的截圖,想觸發(fā)類似系統(tǒng)的截屏功能,找了一圈,總結(jié)一下靠譜的幾種方式。
我寫了個(gè)UIView 的category,將這幾種方式封裝和簡(jiǎn)化了一下。
第一種情形截圖
這種是最最普通的截圖,針對(duì)一般的視圖上添加視圖的情況,基本都可以使用。
源碼:
/**
普通的截圖
該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渲染出來(lái)的,那么使用上面的方式就無(wú)法截圖到OpenGL渲染的部分,這時(shí)候就要用到改進(jìn)后的截圖方案:
/**
針對(duì)有用過(guò)OpenGL渲染過(guò)的視圖截圖
@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 上后,使用上面的幾種方式都無(wú)法截取到Layer上的內(nèi)容,這個(gè)時(shí)候可以使用系統(tǒng)的一個(gè)API,但是該API只能返回一個(gè)UIView,返回的UIView 可以修改frame 等參數(shù)。
/**
截圖
以UIView 的形式返回(_UIReplicantView)
@return 截取出來(lái)的圖片轉(zhuǎn)換的視圖
*/
- (UIView *)snapshotView
{
UIView *snapView = [self snapshotViewAfterScreenUpdates:YES];
return snapView;
}
遺留問(wèn)題:
通過(guò)方式三截取的UIView,無(wú)法轉(zhuǎn)換為UIImage,我試過(guò)將返回的截圖View寫入位圖再轉(zhuǎn)換成UIImage,但是返回的UIImage 要么為空,要么沒(méi)有內(nèi)容。如果有人知道解決方案請(qǐng)告知我。
UIWebView的截圖
去年在做藍(lán)牙打印的時(shí)候,嘗試過(guò)將UIWebView 的內(nèi)容轉(zhuǎn)換為UIImage,寫過(guò)一個(gè)UIWebView的category,也算是對(duì)UIWebView 的截圖,順便也貼出來(lái)吧
- (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.截取的圖片保存起來(lái)
[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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線
本篇文章主要介紹了iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
IOS開發(fā)之路--C語(yǔ)言數(shù)組和字符串
數(shù)組在C語(yǔ)言中有著特殊的地位,它有很多特性,例如它的存儲(chǔ)是連續(xù)的,數(shù)組的名稱就是數(shù)組的地址等。而在C語(yǔ)言中是沒(méi)有String類型的,那么如果要表示一個(gè)字符串,就必須使用字符串?dāng)?shù)組2014-08-08
針對(duì)iOS開發(fā)的一些Xcode使用技巧小結(jié)
這篇文章主要介紹了針對(duì)iOS開發(fā)的一些Xcode使用技巧小結(jié),Xcode是Mac上編寫iOS應(yīng)用的開發(fā)環(huán)境,需要的朋友可以參考下2015-12-12
iOS NSURLSessionDownloadTask實(shí)現(xiàn)文件斷點(diǎn)下載的方法
本篇文章主要介紹了iOS NSURLSessionDownloadTask實(shí)現(xiàn)文件斷點(diǎn)下載的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

