如何在 ios9 中捕获视图的完整屏幕截图
how to capture complete screenshot of view in ios9
捕获屏幕截图是使用下面的代码
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(screenSize, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但它给出了错误>>:CGImageCreateWithImageProvider:无效的图像提供者:NULL。
我不明白这是什么。
请帮忙!!
如何在 ios9
中截取屏幕截图
UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width,self.view.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
请尝试以下功能,它非常适合我:
-(void) TakeScreenShot
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];
}
另外如果你想模仿截图动画请参考这个link,我已经提供了代码
Imitating screenshot flash animation on iOS
解决方案一:
自 iOS 7 以来添加了新的 API,这应该提供获取快照的有效方法
snapshotViewAfterScreenUpdates
:将视图呈现为 UI具有不可修改内容的视图
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets :
同样的东西,但带有可调整大小的插图
drawViewHierarchyInRect:afterScreenUpdates:
:如果你也需要绘制所有子视图(如标签、按钮...),同样如此
您可以使用为任何 UI 效果返回的 UIView,或者如果您需要导出,则可以像之前那样渲染到图像中。
方案二:
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
捕获屏幕截图是使用下面的代码
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(screenSize, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但它给出了错误>>:CGImageCreateWithImageProvider:无效的图像提供者:NULL。
我不明白这是什么。
请帮忙!! 如何在 ios9
中截取屏幕截图UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width,self.view.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
请尝试以下功能,它非常适合我:
-(void) TakeScreenShot
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];
}
另外如果你想模仿截图动画请参考这个link,我已经提供了代码
Imitating screenshot flash animation on iOS
解决方案一:
自 iOS 7 以来添加了新的 API,这应该提供获取快照的有效方法
snapshotViewAfterScreenUpdates
:将视图呈现为 UI具有不可修改内容的视图
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets :
同样的东西,但带有可调整大小的插图
drawViewHierarchyInRect:afterScreenUpdates:
:如果你也需要绘制所有子视图(如标签、按钮...),同样如此
您可以使用为任何 UI 效果返回的 UIView,或者如果您需要导出,则可以像之前那样渲染到图像中。
方案二:
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}