Snapshotted view dont contain image on iOS 10

63 Views Asked by At

I'm dealing with snapshotting of UIView. When i do this for iOS 12 for example, all is ok and output contains snapshot (at least it visible on screen). But when i do this for iOS 10 snapshot is empty. The snapshot have similar size as original view but it's transparent.

Code:

override func viewDidLoad() {
    super.viewDidLoad()

    let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    v.backgroundColor = .red

    view.addSubview(v)

    if let shot = v.snapshotView(afterScreenUpdates: true) {
        shot.frame.origin.y = 110
        view.addSubview(shot)
    }
}
1

There are 1 best solutions below

0
Andrey M. On

I found that on iOS 10 this function somehow called on background queue. Thats why here we must do this:

DispatchQueue.main.async {
    if let shot = v.snapshotView(afterScreenUpdates: true) {
        shot.frame.origin.y = 110
        view.addSubview(shot)
    }
}