Swift 特定viewのスクリーンショットをとる

個人開発したアプリの宣伝
目的地が設定できる手帳のような使い心地のTODOアプリを公開しています。
Todo with Location

Todo with Location

  • Yoshiko Ichikawa
  • Productivity
  • Free

スポンサードリンク

特定Viewのスクリーンショットを撮る。

UIGraphicsBeginImageContextWithOptionsにスクリーンショットを撮る座標と縦横幅を指定する。よって対象のViewのframeを指定すればよい。

UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0)
view.drawHierarchy(in: view.frame, afterScreenUpdates: true)
let screenShotImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!  //スリーンショットがUIImage型で取得できる
UIGraphicsEndImageContext()