Resolved Why is swift cropping the largest part of the image away?

Discussion in 'iOS Programming' started by grandM, Jan 2, 2016.

  1. grandM macrumors 6502a


    Oct 14, 2013
    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.
    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)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            print("scaled image is \(newImage.size.width), \(newImage.size.height)")
            UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
            return newImage
  2. grandM thread starter macrumors 6502a


    Oct 14, 2013
    Ok it got solved after five hours of cursing. Apparantly if you use the simulator and set the editing of the Image Picker on it gets cropped standard. I'm officially pissed at apple.

Share This Page