I have a UIImageView in my app and I'm trying to make it display a wide, short picture. I'm trying to preserve the aspect ratio of the original image.
Here is my code. It currently does not work the way I want it to:
func scaleImage(oldImage: UIImage) -> UIImage {
// 1. Use only y=273 to y=299
let x = 0
let y = Int(oldImage.size.height/2)
let width = Int(oldImage.size.width)
let height = 150
let cropRect = CGRect(x: y, y: x, width: height, height: width)
let cgImage = CGImageCreateWithImageInRect(oldImage.CGImage, cropRect)
var image = UIImage(CGImage: cgImage!, scale:oldImage.scale, orientation: oldImage.imageOrientation)
UIImageWriteToSavedPhotosAlbum(oldImage, nil, nil, nil)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.image = image
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.frame.size.height = image.size.height
}
This is the edited image that was saved to the camera roll: https://i.stack.imgur.com/S3AwU.png
This is what was displayed on the UIImageView: https://i.stack.imgur.com/KX3OF.png
As you can see, the image is cut off on the sides. What am I missing here?