Take snapshot / screenshot of whole UIView - iPhon

2019-07-19 04:49发布

问题:

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);

回答1:

try this:

CGSize fittingSize = [yourScrollView sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(fittingSize);
[yourScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


回答2:

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:

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);


回答4:

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);