我正在尝试拍摄UIView的截图/快照。我可以使用当前可见视图下面的代码获取屏幕截图 - 但我是在整个视图之后。
该视图是一个滚动视图,因此下面的内容是不可见的,我想包含
如何扩展此代码以考虑不可见的视图?
UIGraphicsBeginImageContext(activityView.frame.size);
[self.activityView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
答案 0 :(得分:3)
试试这个:
CGSize fittingSize = [yourScrollView sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(fittingSize);
[yourScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 1 :(得分:3)
iOS 7为类UIView
引入了一种新方法。
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;
- (UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)capInsets;
您可以使用此方法截取当前视图的屏幕截图。
答案 2 :(得分: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);
答案 3 :(得分:0)
我发现了这个问题。 scrollview是我拍摄快照的视图的子视图。为了解决这个问题,我深入了解了正确的观点。
UIGraphicsBeginImageContext(activityView.scrollView.scrollableView.frame.size);
[self.activityView.scrollView.scrollableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);