-1

I have this UIImage resizing extension

extension UIImage {

    func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
        let size = image.size

        let widthRatio  = targetSize.width  / image.size.width
        let heightRatio = targetSize.height / image.size.height

        // Figure out what our orientation is, and use that to form the rectangle
        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
        }

        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        image.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
}

I tried to resize an image by calling the extension like below

 let logoView: UIImageView = {
    let LV = UIImageView()
    let thumbnail = resizeImage(image: "DN", CGSize.init(width:70, height:70))
    LV.image = thumbnail
    LV.contentMode = .scaleAspectFill
    LV.layer.masksToBounds = true
    return LV
}()

However the Xcode is not allowing me to call the resizing function extension. How can I properly resize the image?

   func setupViews() {


    addSubview(logoView)
  }
Ola
  • 431
  • 1
  • 8
  • 28

1 Answers1

2

Functions in extensions are not standalone functions but tied to the thing they extend. In your case, you're adding a function to UIImage but you are calling it like a standalone function.

To fix, your function should be like this:

extension UIImage {

    func resizeImage(targetSize: CGSize) -> UIImage {
        // the image is now “self” and not “image” as you original wrote
        ...
    }
}

and you would call it like:

let logoView: UIImageView = {
    let LV = UIImageView()
    let image = UIImage(named: "DN")
    if let image = image {
        let thumbnail = image.resizeImage(CGSize.init(width:70, height:70))
        LV.image = thumbnail
        LV.contentMode = .scaleAspectFill
        LV.layer.masksToBounds = true
    }
    return LV
}()
Gary Makin
  • 3,109
  • 1
  • 19
  • 27
  • in the function you can reference the image with self – muescha Apr 12 '17 at 00:47
  • 1
    Good point, I'll edit my answer to reflect that. Thanks @muescha – Gary Makin Apr 12 '17 at 00:50
  • thank you for the answer but I still can't get it to work. I declared it as you stated but i can't get it to work inside logoView. I also redefined logoView from UIImageView to UIImage but it would not let me add UIImage as a subview. How do I call it inside UIImageView or how else can it be implemented? – Ola Apr 12 '17 at 13:04
  • I've edited my answer to have more code in it. See if that helps. But, you cannot add a `UIImage` as a subview as it is not a view. That is the role of `UIImageView`. – Gary Makin Apr 12 '17 at 21:05