Last active
May 22, 2017 11:12
-
-
Save ryokosuge/ff6d300741eec31e2ee3 to your computer and use it in GitHub Desktop.
【Swift】UIImageをリサイズする ref: http://qiita.com/ryokosuge/items/d997389529faffab33ba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImageExtension { | |
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, 2) | |
drawInRect(CGRect(x: 0, y: 0, width: resizedSize.width, height: resizedSize.height)) | |
let resizedImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return resizedImage | |
} | |
// 比率だけ指定する場合 | |
func resize(#ratio: CGFloat) -> UIImage { | |
let resizedSize = CGSize(width: Int(self.size.width * ratio), height: Int(self.size.height * ratio)) | |
UIGraphicsBeginImageContextWithOptions(resizedSize, false, 2) | |
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