1

I usually scale the image of an imageView in Android with the scaleX and scaleY properties.

How can I do the same in IOS? I'm really new with IOS,I'm using swift 3 and I didn't find any analog thing like that.

GMX
  • 950
  • 1
  • 14
  • 29
  • This link may help you. http://stackoverflow.com/questions/31966885/ios-swift-resize-image-to-200x200pt-px – T. W Dec 27 '16 at 20:55

1 Answers1

1

If you need to scale the image proportionally, the following code might work for you:

if let cgImage = imageView.image?.cgImage, let orientation = imageView.image?.imageOrientation {
    imageView.image = UIImage(cgImage: cgImage, scale: newScale, orientation: orientation)
}

However, if you only want to display it with a size different than that of the original image, you should control the size of the image using the size of the UIImageView that presents it. To make sure the image scales correctly with the size of the view, take a look at the UIImageView.contentMode property: https://developer.apple.com/reference/uikit/uiview/1622619-contentmode

crizzis
  • 9,978
  • 2
  • 28
  • 47
  • I tried your code but doesn't change anything, i just changed: newScale with 0.5 – GMX Dec 28 '16 at 21:47
  • Is `contentMode` set to `Scale to Fill`? If so, the `UIImage` might be rescaled back to fill the `UIImageView`. As I said, it is the size of the `UIImageView` that drives the scale of the presented `UIImage`, not the other way around (unless you choose a `contentMode` that does not cause image resizing, such as `center`). – crizzis Dec 28 '16 at 21:53
  • What constraints have you defined on your `UIImageView`? – crizzis Dec 28 '16 at 21:56
  • I changed contentMode and now work fine! This is the solution I was looking for but how can I scale only the Y or only the X? Is possible? – GMX Dec 28 '16 at 22:06
  • It's possible but much more complicated. Do you need the scaled `UIImage` for any other purpose than displaying it? If not, do you know the expected size of the image beforehand? – crizzis Dec 28 '16 at 22:12
  • I know the size of the image beforehand and I want to resize it for a better looking purpose. But if is too complicated I think I'd do better to do it with photoshop... – GMX Dec 28 '16 at 22:20
  • If you know it beforehand, just add a width and a height constraint to `UIImageView` (to match the desired width and height of the image) and set `contentMode` back to `Scale to Fill` – crizzis Dec 28 '16 at 22:22