Search code examples
iosswiftparse-platformuiimageavcapturesession

Doesn't Save Desired Image Orientation


When I take a picture from my AVCaptureSession using this method...

func didPressTakePhoto(){
    toggleFlash()

    if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            (sampleBuffer, error) in

            if sampleBuffer != nil {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                self.capturedImage = UIImage(data: imageData)

                if self.camera == true {
                    self.capturedImage = UIImage(CGImage: self.capturedImage.CGImage!, scale: 1.0, orientation: UIImageOrientation.Right)


                } else {
                    self.capturedImage = UIImage(CGImage: self.capturedImage.CGImage!, scale: 1.0, orientation: UIImageOrientation.LeftMirrored)

                }

                self.tempImageView.clipsToBounds = true
                self.tempImageView.image = self.capturedImage
                self.tempImageView.hidden = false
                self.goButton.hidden = false
                self.cameraView.hidden = true
                self.removeImageButton.hidden = false
                self.captureButton.hidden = true

            }


        })
    }
}

I rotate the image to fix the orientation so it looks how it's supposed to look on the image view. When I then save it as a PFFile to Parse it completely messes up the orientation by rotating it 90 degrees to the left. Here is where I save the image to parse...

@IBAction func go(sender: UIButton) {
    let newUser = PFUser()
    newUser["email"] = emailString
    newUser.password = passwordString
    newUser.username = usernameString

    let imageData = UIImagePNGRepresentation(tempImageView.image)

    let imageFile = PFFile(name: "avatar.png", data: imageData!)

    newUser["avatar"] = imageFile

    newUser.signUpInBackgroundWithBlock({ (success, error) -> Void in
        if success != true {

            let signUpAlert = UIAlertController(title: "Couldn't Sign Up!", message:
                "An error occured please try again.", preferredStyle: UIAlertControllerStyle.Alert)

            signUpAlert.addAction(UIAlertAction(title: "Try Again", style: .Default, handler: { (action: UIAlertAction!) -> Void in
                sender.sendActionsForControlEvents(.TouchUpInside)
            }))

            signUpAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) -> Void in
            }))


            self.presentViewController(signUpAlert, animated: true, completion: nil)

        }

    })

}

Please tell me if more information is needed. Thank you!


Solution

  • Parse is not doing anything with your image. When you are rotating a JPEG image in UIImage, the JPEG image data is not being rotated. Instead the orientation is stored in its EXIF metadata. When you convert your image to PNG to save on Prase, EXIF metadata is lost as PNGs do not store any orientation metadata information. In order to get the image in the correct orientation and mirroring, set the correct orientation and mirroring properties to your video connection before capturing the image. Alternatively you can store your image as a JPEG instead of a PNG.