0
我想在顯示MyFirstViewController的同時截取MySecondViewController.view。我不希望MySecondViewController隨時出現在屏幕上。 這可能嗎?不顯示它的屏幕截圖
這是我目前MyFirstViewController的函數內部代碼:
func createPreview() {
let mySecondViewController = self.storyboard!.instantiateViewController(withIdentifier: "MySecondViewController")
self.navigationController?.pushViewController(mySecondViewController, animated: false)
let snapshot = mySecondViewController.view.snapshot()
self.navigationController?.popViewController(animated: false)
}
這裏是我使用的UIView擴展:
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
有了這個代碼,該snapshot
變量包含一個不可見的圖片與好的尺寸。我認爲在視圖從導航控制器中彈出後正在截取屏幕截圖。
如果我試着將afterScreenUpdates
的值設置爲false,那麼結果是一樣的。然後,我認爲它在視圖已被推入導航控制器之前截圖。