本篇文章給大家分享的是有關怎么在iOS中對view進行截圖,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
需要對WKWebView進行截圖,之前用的是下面的方法,高版本的系統是沒有問題的,低版本的卻截到一張白圖
- (UIImage *)convertViewToImage:(UIView *)view{ // 第二個參數表示是否非透明。如果需要顯示半透明效果,需傳NO,否則YES。第三個參數就是屏幕密度了 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; }
查看了之后發現是層級有問題,沒有截到最上面的視圖,于是改為下面的方法就行了
- (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; }
以上就是怎么在iOS中對view進行截圖,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。