LoginSignup
2
4

More than 5 years have passed since last update.

コピペで出来るUIImageの画像サイズを変更(リサイズ)

Posted at

Swift4.0で動作確認済みです

UIimageのextensionを作る

extension UIImage {
    func ResizeUIImage(width : CGFloat, height : CGFloat)-> UIImage!{
        // 引数の画像の大きさのコンテキスト作成
        UIGraphicsBeginImageContext(CGSize(width: width, height: height))
        // コンテキストに画像を描く
        self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        // コンテキストからUIImageを作成
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // コンテキストを閉じる
        UIGraphicsEndImageContext()
        return newImage
    }
}

使用方法

// リサイズする
let resizeImage: UIImage = image.ResizeUIImage(width: 100, height: 100)
2
4
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
2
4