UIScrollView 截图

- (IBAction) renderScrollViewToImage
    {
        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) {
            [UIImagePNGRepresentation(image) writeToFile: @"/tmp/test.png" atomically: YES];
            system("open /tmp/test.png");
        }
    }

posted @ 2015-10-19 16:59  孤独的根号下三  阅读(170)  评论(0编辑  收藏  举报