2015-11-06 62 views
1

當我用這個方法拍攝一張照片從我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) 

     } 

    }) 

} 

請告訴我,如果需要更多信息。謝謝!

回答

2

解析不會對您的圖像做任何事情。當您在UIImage中旋轉JPEG圖像時,JPEG圖像數據不會被旋轉。相反,方向​​存儲在其EXIF元數據中。當您將圖像轉換爲PNG以保存在Prase上時,EXIF元數據將丟失,因爲PNG不存儲任何方向元數據信息。爲了以正確的方向獲取圖像並進行鏡像,請在捕獲圖像之前將正確的方向和鏡像屬性設置爲視頻連接。或者,您可以將圖像存儲爲JPEG而不是PNG。