我想知道我的iPhone应用程序如何将特定UIView
的屏幕截图作为UIImage
。
我试过这个代码,但我得到的只是一个空白图像。
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer drawInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myUIView
的尺寸为320x480,它有一些子视图。执行此操作的正确方法是什么?
发布于 2010-02-07 07:38:00
我想你可能想要renderInContext
,而不是drawInContext
。drawInContext更像是一种你会重写的方法...
请注意,它可能不是在所有视图中都有效,特别是一年前左右,当我尝试在实时摄像机视图中使用它时,它不起作用。
发布于 2014-01-20 17:37:27
从iOS7开始,我们有以下默认方法:
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
调用上述方法比自己尝试将当前视图的内容渲染为位图图像更快。
如果要对快照应用图形效果,请改用drawViewHierarchyInRect:afterScreenUpdates:
方法。
发布于 2018-05-01 21:31:49
iOS 10提供了新的应用编程接口
extension UIView {
func makeScreenshot() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: self.bounds)
return renderer.image { (context) in
self.layer.render(in: context.cgContext)
}
}
}
https://stackoverflow.com/questions/2214957
复制相似问题