Take snapshot / screenshot of whole UIView - iPhon

2019-07-19 04:27发布

I am trying to take a screenshot/snapshot of a UIView. I am able to get the a screenshot with the code below of the current visible view - but i am after the whole view.

The view is a scrollview so there is content below that isnt visible that i would like to be included

How do i extend this code to take into account the view that isnt visible?

UIGraphicsBeginImageContext(activityView.frame.size);

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

UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);

4条回答
孤傲高冷的网名
2楼-- · 2019-07-19 04:55

There is a new method introduced from iOS 7 for a class UIView.

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

You can use this method to take screenshots of your current view.

查看更多
虎瘦雄心在
3楼-- · 2019-07-19 04:56

try this:

CGSize fittingSize = [yourScrollView sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(fittingSize);
[yourScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
查看更多
仙女界的扛把子
4楼-- · 2019-07-19 04:57

try this way may be helped you

    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);
查看更多
何必那么认真
5楼-- · 2019-07-19 05:02

I have found the problem. The scrollview was a subview of the view i was taking a snapshot of. To fix it i drilled down to the correct view.

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

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

UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
查看更多
登录 后发表回答