2

我正在尝试截取 UIView 的屏幕截图/快照。我可以使用以下代码获取当前可见视图的屏幕截图 - 但我关注的是整个视图。

该视图是一个滚动视图,因此下面的内容是不可见的,我想被包含在内

我如何扩展此代码以考虑不可见的视图?

UIGraphicsBeginImageContext(activityView.frame.size);

[self.activityView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
4

4 回答 4

3

iOS 7 为类引入了一种新方法UIView

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;
 - (UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)capInsets;

您可以使用此方法截取当前视图的屏幕截图。

于 2013-11-01T06:38:15.817 回答
3

尝试这个:

CGSize fittingSize = [yourScrollView sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(fittingSize);
[yourScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2012-09-10T11:47:05.640 回答
1

试试这个方法可能对你有帮助

    UIImage *screenshotImage;
        UIGraphicsBeginImageContext(self.view.bounds.size);
        [self drawLayer:self.view.layer inContext:UIGraphicsGetCurrentContext()];
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
        img.image=screenshotImage;
        UIGraphicsEndImageContext();
        CGImageRef cgImg = CGImageCreateWithImageInRect(screenshotImage.CGImage, CGRectMake(0, 0, 320, 460));
        UIImage* part = [UIImage imageWithCGImage:cgImg];
        UIImageView* iv = [[UIImageView alloc] initWithImage:part];

        UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
于 2012-09-10T11:46:24.860 回答
0

我发现了问题。滚动视图是我正在拍摄快照的视图的子视图。为了修复它,我深入研究了正确的视图。

UIGraphicsBeginImageContext(activityView.scrollView.scrollableView.frame.size);

[self.activityView.scrollView.scrollableView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
于 2012-09-10T11:59:31.580 回答