ios - 不保存所需的图像方向

标签 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

            }


        })
    }
}

我旋转图像以固定方向,使其看起来像在 ImageView 中应该看起来的样子。当我将它另存为 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)

        }

    })

}

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

最佳答案

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

关于ios - 不保存所需的图像方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557563/

相关文章:

ios - 如何快速删除导航栏的边框?

跨所有 View Controller 的 iOS 自定义 View

ios - 如何从其声明之外调用 block ?

ios - NSUserdefault 不适用于 PFObject

php - 使用 Composer 要求多个子目录共享文件

ios - 在下载时从 NSURLSessionDownloadTask 访问字节

ios - 我什么时候应该使用conformsToProtocol和respondsToSelector方法?

cocoa - 为想要开始为 Cocoa Touch/iOS 开发的 Cocoa/Mac 程序员的快速入门指南/备忘单指南的指针?

ios - 在SwiftUI中手动设置亮/暗模式并保存用户选择

ios - 从 Parse 服务器获取数据到 xcode