开发者

Capturing UIScrollView off screen content

I'm working with a UIScrollView which can be used in either orientation and contains a number of subviews:

// in viewDidLoad

mainScrollView = (UIScrollView *)self.view;

mainScrollView.contentSize = CGSizeMake(1024, 1432);

I'm trying to capture a screen shot of the entire scroll view with all its subviews, including what is not currently visible on screen. The following captures only what is visible on screen at the time of capture:

// make screenshot

 UIGraphicsBeginImageContext(mainScrollView.bounds.size);

 [mainScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];

 UIImage *screenImg = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();



 // save screenshot in docs dir

 NSData *pngData = UIImagePNGRepresentation(screenImg);

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 NSString *documentsDir = [paths objectAtIndex:0];

 [pngData writeToFile:[documentsDir stringByAppendingPathComponent:@"screen1.png"] 

                 options:NSDataWritingAtomic error:nil];

Adding the following before making the screen shot was the only way I could fine that enabled me to capture the entire scroll view:

 // capture off-screen content

 mainScrollView.frame = CGRectMake(0, 0, 1024, 1432);

This capture is fine and includes all subviews. The problem is that once I have made the screen shot, my scroll view becomes unresponsive so I can no longer scroll (or pan sideways if in portrait mode). If I rotate the device, the problem seems to rectify itself and I can scroll and pan normally. I'm sure I'm missing something very simple that is needed to get my scroll view worki开发者_如何学Cng normally straight after the screen shot is taken. Any suggestions much appreciated!


The problem is that you are setting the frame size equals to the content size. When the frame size is equals or greater to the content size you can't scroll. You have to find a way to identify when the screen shot have finished and change the frame size back to the previous size. So:

CGRect oldFrame = mainScrollView.frame;

// do your logic to screenshot

mainScrollView.frame = oldFrame;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜