tableview 截圖

tableview 截圖

//將tableview 生成圖片

+ (UIImage*)getTableViewimage:(UITableView*)tableView

{

? ? if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){

? ? ? ? UIGraphicsBeginImageContextWithOptions(tableView.contentSize, NO, [UIScreen mainScreen].scale);


? ? }else{

? ? ? ? UIGraphicsBeginImageContext(tableView.contentSize);

? ? }

? ? //先保存原來frame 和 偏移量

? ? CGPointsavedContentOffset = tableView.contentOffset;

? ? CGRectsavedFrame =tableView.frame;


? ? CGSizecontentSize = tableView.contentSize;


? ? CGRectoldBounds = tableView.bounds;


? ? if(@available(iOS13, *)){

? ? ? ? //iOS 13 系統(tǒng)截屏需要改變tableview 的bounds

? ? ? ? [tableView.layersetBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, contentSize.width, contentSize.height)];

? ? }


? ? //偏移量歸零

? ? tableView.contentOffset = CGPointZero;

? ? //frame變?yōu)閏ontentSize

? ? tableView.frame = CGRectMake(0, 0,tableView.contentSize.width, tableView.contentSize.height);


? ? //截圖

? ? [tableView.layer renderInContext:UIGraphicsGetCurrentContext()];

? ? if(@available(iOS13,*)){

? ? ? ? [tableView.layersetBounds:oldBounds];

? ? }

? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? //還原frame 和 偏移量

? ? tableView.contentOffset= savedContentOffset;

? ? tableView.frame= savedFrame;


? ? returnimage;


}

方法沒問題,但是一直出現(xiàn)超出屏幕部分截圖失敗問題,百度很多,都么解決

結果發(fā)現(xiàn)是調了viewDidLayoutSubviews方法,害

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容