Xcode>File>Project>Gameで生成された、GameViewController.swiftの中でスクリーンショットを撮ろうとした。
#失敗したコード
調べてみると、以下の記事が出てきた。
How to take Full screen Screenshot in swift?
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
しかしこれを実行してもキャプチャは真っ白でうまく撮れていなかった。
#うまくいったコード
今度はこちらの記事を読んだ
Screenshot in swift iOS?
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
上のコードを実行するとうまくできた。
なぜかはわかりませんでした・・・。