LoginSignup
5
6

More than 5 years have passed since last update.

UIViewからUIImageを生成する

Last updated at Posted at 2016-10-26

Problem

  • UIViewからUIImageを作成したい。
  • UIImageの背景は透明にしたい。
  • 画質は低下させない。

Solution

# Swfit2.3

import UIKit

extension UIView {
    func snapshotImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
        guard let currentContext = UIGraphicsGetCurrentContext() else {
            UIGraphicsEndImageContext()
            return nil
        }
        layer.renderInContext(currentContext)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

Ref

5
6
2

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
5
6