func resizeImage()

in FriendlyPix/UIImage+Circle.swift [37:66]


  func resizeImage(_ dimension: CGFloat) -> UIImage {
    var width: CGFloat
    var height: CGFloat
    var newImage: UIImage

    let size = self.size
    let aspectRatio = size.width / size.height

    if aspectRatio > 1 {                            // Landscape image
      width = dimension
      height = dimension / aspectRatio
    } else {                                        // Portrait image
      height = dimension
      width = dimension * aspectRatio
    }

    if #available(iOS 10.0, *) {
      let renderFormat = UIGraphicsImageRendererFormat.default()
      let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: renderFormat)
      newImage = renderer.image { _ in
        self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
      }
    } else {
      UIGraphicsBeginImageContext(CGSize(width: width, height: height))
      self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
      newImage = UIGraphicsGetImageFromCurrentImageContext()!
      UIGraphicsEndImageContext()
    }
    return newImage
  }