Skip to content

Instantly share code, notes, and snippets.

@zlq4863947
Last active February 27, 2018 09:46
Show Gist options
  • Save zlq4863947/ac95715a1c4d1e548debb7615f714732 to your computer and use it in GitHub Desktop.
Save zlq4863947/ac95715a1c4d1e548debb7615f714732 to your computer and use it in GitHub Desktop.
【Swift4】UIImageで画像のサイズ変更、指定した倍率で拡大/縮小 ref: https://qiita.com/zlq4863947/items/4b016824cf152bc2a769
extension UIImage {
// resize image
func reSizeImage(reSize:CGSize)->UIImage {
//UIGraphicsBeginImageContext(reSize);
UIGraphicsBeginImageContextWithOptions(reSize,false,UIScreen.main.scale);
self.draw(in: CGRect(x: 0, y: 0, width: reSize.width, height: reSize.height));
let reSizeImage:UIImage! = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
// scale the image at rates
func scaleImage(scaleSize:CGFloat)->UIImage {
let reSize = CGSize(width: self.size.width * scaleSize, height: self.size.height * scaleSize)
return reSizeImage(reSize: reSize)
}
}
let reSize = CGSize(width: self.size.width * scaleSize, height: self.size.height * scaleSize)
image?.reSizeImage(reSize: reSize)
image?.scaleImage(scaleSize: 0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment