最近做項目遇到要求截取圖片長度超出手機屏幕,即可滑動的長圖截屏,這里簡單說一下解決思路:
當我們要截全屏時,將滑動視圖的frame以及偏移量記錄下來,然后將滑動視圖偏移量設為0,frame改為滑動視圖的
contentSize,然后生成圖片進行保存,代碼實現如下:
// 下面方法,第一個參數表示區域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數就是屏幕密度了,調整清晰度。 UIGraphicsBeginImageContextWithOptions(scroll.contentSize, YES, [UIScreen mainScreen].scale); CGPoint savedContentOffset = scroll.contentOffset; CGRect savedFrame = scroll.frame; scroll.contentOffset = CGPointZero; scroll.frame = CGRectMake(0, 0, scroll.contentSize.width, scroll.contentSize.height); [scroll.layer renderInContext: UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); scroll.contentOffset = savedContentOffset; scroll.frame = savedFrame; UIGraphicsEndImageContext();