LoginSignup
19
18

More than 5 years have passed since last update.

[Swift]Spritekitでスクリーンショットを撮る

Posted at

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()

上のコードを実行するとうまくできた。

なぜかはわかりませんでした・・・。

19
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18