不保存所需的图像方向

时间:2015-11-06 00:44:11

标签: ios swift parse-platform uiimage avcapturesession

当我使用这种方法从我的AVCaptureSession拍照时......

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

            }


        })
    }
}

我旋转图像以固定方向,使其看起来应该在图像视图上看起来如何。然后,当我将它保存为PFFile以解析它时,通过向左旋转90度完全弄乱了方向。这是我保存图像以解析的地方......

@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)

        }

    })

}

如果需要更多信息,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:2)

Parse没有对你的图像做任何事情。在UIImage中旋转JPEG图像时,JPEG图像数据不会旋转。相反,方向​​存储在其EXIF元数据中。当您将图像转换为PNG以保存在Prase上时,EXIF元数据会丢失,因为PNG不存储任何方向元数据信息。为了使图像处于正确的方向和镜像,请在捕获图像之前为视频连接设置正确的方向和镜像属性。或者,您可以将图像存储为JPEG而不是PNG。