-(UIImage*)captureView:(UIView *)theView{
CGRect rect = theView.frame;
if ([theView isKindOfClass:[UIScrollView class]]) {
rect.size = ((UIScrollView *)theView).contentSize;
}
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (UIImage *)captureScrollView:(UIScrollView *)scrollView{
UIImage* image = nil;
UIGraphicsBeginImageContext(scrollView.contentSize);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(, , scrollView.contentSize.width, scrollView.contentSize.height); [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext(); scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext(); if (image != nil) {
return image;
}
return nil;
}
60down voteaccepted
|
Here is code that works ...
The last few lines simply write the image to /tmp/test.png and then opens it in Preview.app. This obviously only works on in the Simulator :-) Complete project in the ScrollViewScreenShot Github Repository |