scale image to smaller size in swift3 [closed] scale image to smaller size in swift3 [closed] swift swift

scale image to smaller size in swift3 [closed]


Use this extension to resize your image:

extension UIImage{func resizeImageWith(newSize: CGSize) -> UIImage {    let horizontalRatio = newSize.width / size.width    let verticalRatio = newSize.height / size.height    let ratio = max(horizontalRatio, verticalRatio)    let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)    UIGraphicsBeginImageContextWithOptions(newSize, true, 0)    draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage!    }}

Basically you are calculating the aspect ratio to keep the image intact while resizing it. Then you are getting the current image context and drawing it in the specified rectangle and finally returning it as a new image.


 func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {    let scale = newWidth / image.size.width    let newHeight = image.size.height * scale    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage}@IBAction func chooseImage(sender: AnyObject) {    var myPickerController = UIImagePickerController()    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary    myPickerController.delegate = self;    self.presentViewController(myPickerController, animated: true, completion: nil)}func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){    var image = info[UIImagePickerControllerOriginalImage] as? UIImage    let scaledImage:UIImage = resizeImage(image!, newWidth: 200)    self.dismissViewControllerAnimated(true, completion: nil)}

Swift 4 version

extension UIImage {    func resize(withWidth newWidth: CGFloat) -> UIImage? {        let scale = newWidth / self.size.width        let newHeight = self.size.height * scale        UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))        self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))        let newImage = UIGraphicsGetImageFromCurrentImageContext()        UIGraphicsEndImageContext()        return newImage    }}


Try this:

First Method:

func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {let scale = newWidth / image.size.widthlet newHeight = image.size.height * scaleUIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))let newImage = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return newImage}

Second Method:

import UIKitfunc RBSquareImageTo(image: UIImage, size: CGSize) -> UIImage {  return RBResizeImage(RBSquareImage(image), size)}func RBSquareImage(image: UIImage) -> UIImage {  var originalWidth  = image.size.width  var originalHeight = image.size.height  var edge: CGFloatif originalWidth > originalHeight {    edge = originalHeight} else {    edge = originalWidth}var posX = (originalWidth  - edge) / 2.0var posY = (originalHeight - edge) / 2.0var cropSquare = CGRectMake(posX, posY, edge, edge)var imageRef = CGImageCreateWithImageInRect(image.CGImage, cropSquare);return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation) }func RBResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {let size = image.sizelet widthRatio  = targetSize.width  / image.size.widthlet heightRatio = targetSize.height / image.size.height// Figure out what our orientation is, and use that to form the rectanglevar newSize: CGSizeif(widthRatio > heightRatio) {    newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)} else {    newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)}// This is the rect that we've calculated out and this is what is actually used belowlet rect = CGRectMake(0, 0, newSize.width, newSize.height)// Actually do the resizing to the rect using the ImageContext stuffUIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)image.drawInRect(rect)let newImage = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return newImage  }

Reference:

  1. Here

  2. Here