Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created April 5, 2016 15:13
Show Gist options
  • Save KentarouKanno/5ff7edec54a476ba205479dd3b915e85 to your computer and use it in GitHub Desktop.
Save KentarouKanno/5ff7edec54a476ba205479dd3b915e85 to your computer and use it in GitHub Desktop.
UIImage

UIImage

★ UIImage Extension リサイズメソッド

extension UIImage {
    func resize(size: CGSize) -> UIImage {
        let widthRatio = size.width / self.size.width
        let heightRatio = size.height / self.size.height
        let ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio
        let resizedSize = CGSize(width: (self.size.width * ratio), height: (self.size.height * ratio))
        
        // 画質を落とさないように設定
        UIGraphicsBeginImageContextWithOptions(resizedSize, false, 0.0)
        drawInRect(CGRect(x: 0, y: 0, width: resizedSize.width, height: resizedSize.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return resizedImage
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment