1.普通的截图办法,在View上面截图
/** * 截图代码 * * @param view 需要截图的view * @param rect 需要截取的区域 * * @return 返回截取的对象 */ + (UIImage *)viewSnapshot:(UIView *)view withInRect:(CGRect)rect { UIGraphicsBeginImageContext(view.bounds.size); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage,rect); image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return image; }
2.截取UIScrollView的ContentSize的全部区域,包括未显示的部分,将UIScrollView传递进去
/** * UIScrollView从顶截到底 * * @param scrollView 需要呗截图的UIScrollView * * @return 返回UIScrollView的ContentSize的截图 */ + (UIImage *)screenShot:(UIScrollView *)scrollView{ UIImage *image = nil; UIGraphicsBeginImageContext(scrollView.contentSize); { CGPoint savedContentOffset = scrollView.contentOffset; CGRect savedFrame = scrollView.frame; scrollView.contentOffset = CGPointZero; scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height); [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); scrollView.contentOffset = savedContentOffset; scrollView.frame = savedFrame; } UIGraphicsEndImageContext(); if (image != nil) { NSLog(@"截图成功!"); } return image; }