33
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift4】UIImageで画像のサイズ変更、指定した倍率で拡大/縮小

Last updated at Posted at 2018-02-27

環境

Xcode 9.2
Swift 4.0.3

方法の追加

extensionで画像のサイズ変更指定した倍率で拡大/縮小との二つ方法を追加

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)

0.5倍率で画像を縮小する:

image?.scaleImage(scaleSize: 0.5)
33
21
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
33
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?