1

Here is my purpose, I have an image width of 750 and I want to scale it to 128 Then I found an init method of UIImage called init(data:scale:)

The next is my code

func scaleImage(image:UIImage, ToSpecificWidth targetWidth:Int) -> UIImage{
        var scale =  Double(targetWidth) / Double(image.size.width)
        let scaledImage = UIImage(data: UIImagePNGRepresentation(image)!  as Data, scale: CGFloat(scale))
        return scaledImage!
}

print("originImageBeforeWidth: \(portrait?.size.width)") // output: 750
let newImage = Tools.scaleImage(image: portrait!, ToSpecificWidth: 128) // the scale is about 0.17
print("newImageWidth: \(newImage.size.width)") // output: 4394.53125

apparently the new width is too far away from my intension I'm looking for 750 * 0.17 = 128 But I get 750 / 0.17 = 4394

then I change my scaleImage func Here is the updated code

func scaleImage(image:UIImage, ToSpecificWidth targetWidth:Int) -> UIImage{
        var scale =  Double(targetWidth) / Double(image.size.width)
        scale = 1/scale // new added
        let scaledImage = UIImage(data: UIImagePNGRepresentation(image)!  as Data, scale: CGFloat(scale))
        return scaledImage!
}

print("originImageBeforeWidth: \(portrait?.size.width)") // output: 750
let newImage = Tools.scaleImage(image: portrait!, ToSpecificWidth: 128) // the scale is about 5.88
print("newImageWidth: \(newImage.size.width)") // output: 128

Which is exactly what I want, but the code scale =1/scale doesn't make any sense

What is going on here?

Stanley
  • 1,981
  • 3
  • 14
  • 18

2 Answers2

2

The init method you are trying to use is not for the purpose of resizing an image. And the scale parameter is not a resizing scale. It's the 1x, 2x, 3x scale. Essentially, the only valid values for scale are currently 1.0, 2.0, or 3.0.

While setting the scale to the inverse of what you expected gives you a size property returning your desired result, it is not at all what you should be doing.

There are proper ways to resize an image such as How to Resize image in Swift? as well as others.

Community
  • 1
  • 1
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

According to apple document,

UIImage.scale
The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

UIImage.size
This value reflects the logical size of the image and takes the image’s current orientation into account. Multiply the size values by the value in the scale property to get the pixel dimensions of the image.

so,
real pixel size = size * scale

That's why you need to set 1/scale.

By the way,
scale only affect the size value of UIImage properly.
It mean it only affect the size for showing on screen, not changing pixel size.
If you want to resize, you can draw with scale and use UIGraphicsGetImageFromCurrentImageContext()