LoginSignup
15
14

More than 5 years have passed since last update.

Swiftでスクリーンショットを取得する

Last updated at Posted at 2016-08-04

サンプルコード

ViewController.swift
class SampleProject : UIViewController {

    @IBOutlet weak var screenShot: UIImage!
    var capturedImage : UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }

    private func setup() {
        capturedImage = getScreenShot() as UIImage
        screenShot.image = capturedImage
    }

    private func getScreenShot() -> UIImage {
        let rect = self.view.bounds
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        self.view.layer.renderInContext(context)
        let capturedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return capturedImage
    }
}
15
14
0

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
15
14