1

I'm passing an image to this method in order to scale my image and return an image that isn't horizontal which will be saved in the document directory; however, the method is somehow truncating maybe a quarter of an inch of the side of the image.

Please advise..

   func scaleImageWithImage(image: UIImage, size:CGSize)-> UIImage{
        let scale:CGFloat = max(size.width/image.size.width, size.height/image.size.height)
        let width: CGFloat = image.size.width * scale
        let height: CGFloat = image.size.height * scale
        let imageRect: CGRect = CGRectMake((size.width-width)/2.0, (size.height - height) / 2.0, width, height)

        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        image.drawInRect(imageRect)
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
Walking
  • 467
  • 1
  • 7
  • 23

1 Answers1

0

You are drawing in rect imageRect but the graphics context itself is of size size. Thus, if size is smaller than imageRect.size, you're losing some information at the edge. Moreover, imageRect doesn't start at 0,0 — its origin is (size.width-width)/2.0, (size.height-height)/2.0 — so if its origin is moved positively from the origin you will have blank areas at the other side, and if it is moved negatively from the origin you will lose some information at that edge.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Matt, thank you for your answer. I'm still a bit confused and hope you can clear the confusion. I'm taking an image from the camera and saving it to the document directory to then show it in an ImageView later on. I'm doing `let newImage = scaledImageWithImage(image, size: CGSize(width: screenSize.width, height: screenSize.height))` and saving that. However, there's no way I can demonstrate an image in the ImageView exactly as it was taken. I tried settings the imageRect to 0,0 and that didn't help either. I tried not scaling at all and the image shows sideways! Thank you for your time man – Walking Mar 31 '16 at 18:43
  • I am still completely unclear on what you want to do, or even why you're having a problem in the first place. An image view with the proper content mode will display the entire image, though it is better, indeed, if you do scale it. How are you saving to the document directory? It sounds like you may be throwing away the `orientation` information. – matt Mar 31 '16 at 18:54
  • I'm getting an image, scaling it, and then saving it to directory. `let pathFull = documentsDirectory.stringByAppendingString(photoURL as String) let pngFullData = UIImagePNGRepresentation(newImage) pngFullData?.writeToFile(pathFull, atomically: true)` . I thought I would just be able to retrieve it normally but it seems like when I don't scale, the image comes sideways. When I do scale, it comes back from the directory with the sides missing. Thank you for patience – Walking Mar 31 '16 at 19:07
  • `UIImagePNGRepresentation(newImage)` is the problem. You are, as I said, throwing away the orientation information. Use the ImageIO framework and keep the orientation information as you save out. – matt Mar 31 '16 at 19:14
  • Alternatively you can do it thru the metadata directly, as in this answer http://stackoverflow.com/a/12110427/341994 but ImageIO is better. – matt Mar 31 '16 at 19:17
  • Do you mind pushing me in the right direction in terms of this "ImageIO" way that you're referring to? I can't seem to find anything that will show me how to get image data with imageio .. very likely that I'm looking up the wrong terms – Walking Mar 31 '16 at 19:18
  • Really? https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/ImageIOGuide/ikpg_dest/ikpg_dest.html#//apple_ref/doc/uid/TP40005462-CH219-SW3 – matt Mar 31 '16 at 19:19
  • In some way, the scaling was allowing the image to maintain orientation since it was only flipped when I wouldn't scale. PNGRepresentation was there in both trials. Did the scaling somehow allow the image to maintain it? – Walking Mar 31 '16 at 19:20