Help us understand the problem. What is going on with this article?

6行で UIView を UIImage に変換してサクッと画像を保存する


今回は特定の View を UIImage に変換してフォトライブラリーに保存できるようにします



UIView から image を取得するための ExtensionProperty を追加します。

extension UIView {
    var image: UIImage {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        layer.render(in: context)
        let capturedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        return capturedImage


毎度おなじみの UIImageWriteToSavedPhotosAlbum() で画像が保存できていれば完了です。

    func saveImage() {
        let image = view.image
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away