Capture off screen content as UIImage in iOS7

400 Views Asked by At

I have a UIView within UIScrollView which is much higher than the screen. My code looks like this

UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, -500.0f, 320.0f, 2000.0f)];
[_scrollView addSubview:contentView];

and I want to capture an image of contentView

//Screen capture.
UIGraphicsBeginImageContextWithOptions(contentView.frame.size, NULL, 0.0f);
[self.view drawViewHierarchyInRect:contentView.bounds afterScreenUpdates:NO];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The above code works on simulator but not on actual device (iPod 5th gen)

If I capture the scrollView instead of contentView, it works ok, but only what's displayed on the screen will be captured. Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

Well, I reckon this may not be possible, as iOS won't draw off screen contents in order to gain more performance and battery life.

1
On

Use the scrollView.contentOffset. to get the frame of the View, which is currently on the screen.