iOS 對view進行截圖的示例代碼
本文主要介紹了iOS 對view進行截圖的示例代碼,分享給大家,具體如下:
需要對WKWebView進行截圖,之前用的是下面的方法,高版本的系統(tǒng)是沒有問題的,低版本的卻截到一張白圖
- (UIImage *)convertViewToImage:(UIView *)view{ // 第二個參數(shù)表示是否非透明。如果需要顯示半透明效果,需傳NO,否則YES。第三個參數(shù)就是屏幕密度了 UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height * 0.8),YES,[UIScreen mainScreen].scale); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
查看了之后發(fā)現(xiàn)是層級有問題,沒有截到最上面的視圖,于是改為下面的方法就行了
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)frame{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(theView.bounds.size.width, theView.bounds.size.height*0.8), YES, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); UIImage *img; if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){ for(UIView *subview in theView.subviews){ [subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES]; } img = UIGraphicsGetImageFromCurrentImageContext(); }else{ CGContextSaveGState(context); [theView.layer renderInContext:context]; img = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext(); return img; }
要注意,frame是不能為空的,而且截的太快的話也會有問題,需要設(shè)置afterScreenUpdates為NO,因為設(shè)置為YES后,這些方法會等在view update結(jié)束在執(zhí)行,如果在update結(jié)束前view被release了,會出現(xiàn)找不到view的問題。另外記得使用UIGraphicsBeginImageContextWithOptions,這樣截取出來的是高清圖。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS的UI開發(fā)中UITabBarControlle的基本使用教程
這篇文章主要介紹了iOS的UI開發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較
IOS中獲取網(wǎng)絡(luò)數(shù)據(jù)一般有三種:1、NSURLCondition(已過時) 2、NSURLSession 3、三方庫AFNetWorking。下面通過本文給大家比較這三種方法的區(qū)別對比2017-11-11iOS開發(fā)之tableView實現(xiàn)左滑刪除功能
我們在使用一些應(yīng)用的時候,在滑動一些聯(lián)系人的某一行的時候,會出現(xiàn)刪除、置頂、更多等等的按鈕,下面這篇文章主要就介紹了iOS用tableView實現(xiàn)左劃刪除功能的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2017-01-01