Ok, I'm totally lost. I'm using this code. This code does what it does trying to keep the ratio. If however I use the code swift just crops away (this I understand) but almost everything of the image is lost. That I do not get. I'm lost. I think there is something I do not understand. I did a test requiring the outputimage having the same dimensions as the input image. Almost everything was cropped away.
Code:
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
print("original image is \(image.size.width), \(image.size.height)")
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 = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
print("scaled image is \(newImage.size.width), \(newImage.size.height)")
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
return newImage
}