Screenshot layers in front of UIView

时间:2015-12-10 01:59:41

标签: ios swift uiview screenshot

I have a UIView in my storyboard which I can take a screenshot of using the following:

extension UIView {

func pb_takeSnapshot() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)

    drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

and using it like this: let screenShot = exampleView.pb_takeSnapshot()

But this gives just the blank view. And putting it behind the other views in the view controller doesn't work of course. How do I take a screenshot of the superview using the UIView's position and size?

1 个答案:

答案 0 :(得分:1)

使用UIView的位置和大小结束拍摄超级视图的屏幕截图,而不是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)