iOS 截取tableView長(zhǎng)圖

- (UIImage *)snapshotScreen
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
    UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, NO, [UIScreen mainScreen].scale);
 
} else {
    UIGraphicsBeginImageContext(self.tableView.contentSize);
}
//先保存原來(lái)frame 和 偏移量
CGPoint savedContentOffset = self.tableView.contentOffset;
CGRect savedFrame = self.tableView.frame;
CGSize contentSize = self.tableView.contentSize;
CGRect oldBounds = self.tableView.layer.bounds;

if(@available(iOS 13, *)){
    //iOS 13 系統(tǒng)截屏需要改變tableview 的bounds
     [self.tableView.layer setBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, contentSize.width, contentSize.height)];
}

//偏移量歸零
self.tableView.contentOffset = CGPointZero;
//frame變?yōu)閏ontentSize
self.tableView.frame = CGRectMake(0, 0, self.tableView.contentSize.width, self.tableView.contentSize.height);
//截圖
 [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
if(@available(iOS 13,*)){
    [self.tableView.layer setBounds:oldBounds];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//還原frame 和 偏移量
self.tableView.contentOffset = savedContentOffset;
self.tableView.frame = savedFrame;


//    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
return image;

}

iOS 13系統(tǒng)之前 使用 renderInContext:的api沒(méi)有問(wèn)題,iOS 13之后需要對(duì)當(dāng)前視圖的bounds。
參考文章。

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

相關(guān)閱讀更多精彩內(nèi)容

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