关闭屏幕内容捕获UIScrollView [英] Capturing UIScrollView off screen content

查看:88
本文介绍了关闭屏幕内容捕获UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIScrollView,它可以在任何方向使用并包含许多子视图:

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!

推荐答案

问题是您将帧大小设置为等于内容大小。当帧大小等于或大于内容大小时,您无法滚动。
您必须找到一种方法来识别屏幕截图何时完成并将帧大小更改回以前的大小。所以:

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;

这篇关于关闭屏幕内容捕获UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆