UIView 前面的截图层
Screenshot layers in front of UIView
我的情节提要中有一个 UIView,我可以使用以下内容截屏:
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
并像这样使用它:let screenShot = exampleView.pb_takeSnapshot()
但这只是空白视图。将它放在视图控制器中其他视图的后面当然是行不通的。如何使用 UIView 的位置和大小截取 superview 的屏幕截图?
最终使用我推测的 UIView 的位置和大小截取了 superview 的屏幕截图...而不是 UIView 本身
UIGraphicsBeginImageContextWithOptions(CGSizeMake(exampleView.bounds.size.width, exampleView.bounds.size.height), false, UIScreen.mainScreen().scale)
self.view.drawViewHierarchyInRect(CGRectMake(-exampleView.frame.origin.x, -exampleView.frame.origin.y, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)
我的情节提要中有一个 UIView,我可以使用以下内容截屏:
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
并像这样使用它:let screenShot = exampleView.pb_takeSnapshot()
但这只是空白视图。将它放在视图控制器中其他视图的后面当然是行不通的。如何使用 UIView 的位置和大小截取 superview 的屏幕截图?
最终使用我推测的 UIView 的位置和大小截取了 superview 的屏幕截图...而不是 UIView 本身
UIGraphicsBeginImageContextWithOptions(CGSizeMake(exampleView.bounds.size.width, exampleView.bounds.size.height), false, UIScreen.mainScreen().scale)
self.view.drawViewHierarchyInRect(CGRectMake(-exampleView.frame.origin.x, -exampleView.frame.origin.y, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)