iOS scrollview截图

时间:2017-08-31 08:57:59

标签: ios scrollview screenshot

UIImage* image = nil;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, 0);
{
    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();

我无法获得滚动视图的完整图像,但只有一半,而一半是黑色

1 个答案:

答案 0 :(得分:1)

修改

尝试添加

drawHierarchy(in: self.bounds, afterScreenUpdates: true)
行后

[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];