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);
try this:
CGSize fittingSize = [yourScrollView sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(fittingSize);
[yourScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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.
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);
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);