Capturing UIScrollView off screen content

若如初见. 提交于 2019-12-22 06:18:52

问题


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 working normally straight after the screen shot is taken. Any suggestions much appreciated!


回答1:


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;


来源:https://stackoverflow.com/questions/5611350/capturing-uiscrollview-off-screen-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!