2015-12-18 47 views
0

我用AVCamSwift拍照。但是,當我從內部照相機拍攝照片時,它會將圖像垂直地保存爲垂直。代碼的所有者確認這是一個錯誤。這些是我使用的代碼:AVCamSwift前置攝像頭垂直反轉保存圖像

@IBAction func snapStillImage(sender: AnyObject) { 

    print("snapStillImage") 
    dispatch_async(self.sessionQueue, { 
     // Update the orientation on the still image output video connection before capturing. 

     let videoOrientation = (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation 

     self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation 

     // Flash set to Auto for Still Capture 
     takePhotoScreen.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device) 



     self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: { 
      (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in 

      if error == nil { 
       let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) 
       let image:UIImage = UIImage(data: data)! 

       let libaray:ALAssetsLibrary = ALAssetsLibrary() 
       let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)! 
       libaray.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil) 

       print("save to album") 


      }else{ 
      print("Did not capture still image") 
      print(error) 
      } 


     }) 


    }) 

} 

@IBAction func changeCamera(sender: AnyObject) { 


    print("change camera") 

    self.cameraButton.enabled = false 

    self.snapButton.enabled = false 

    dispatch_async(self.sessionQueue, { 

     let currentVideoDevice:AVCaptureDevice = self.videoDeviceInput!.device 
     let currentPosition: AVCaptureDevicePosition = currentVideoDevice.position 
     var preferredPosition: AVCaptureDevicePosition = AVCaptureDevicePosition.Unspecified 

     switch currentPosition{ 
     case AVCaptureDevicePosition.Front: 
      preferredPosition = AVCaptureDevicePosition.Back 
     case AVCaptureDevicePosition.Back: 
      preferredPosition = AVCaptureDevicePosition.Front 
     case AVCaptureDevicePosition.Unspecified: 
      preferredPosition = AVCaptureDevicePosition.Back 

     } 


     let device:AVCaptureDevice = takePhotoScreen.deviceWithMediaType(AVMediaTypeVideo, preferringPosition: preferredPosition) 

     var videoDeviceInput: AVCaptureDeviceInput? 

     do { 
      videoDeviceInput = try AVCaptureDeviceInput(device: device) 
     } catch _ as NSError { 
      videoDeviceInput = nil 
     } catch { 
      fatalError() 
     } 

     self.session!.beginConfiguration() 

     self.session!.removeInput(self.videoDeviceInput) 

     if self.session!.canAddInput(videoDeviceInput){ 

      NSNotificationCenter.defaultCenter().removeObserver(self, name:AVCaptureDeviceSubjectAreaDidChangeNotification, object:currentVideoDevice) 

      takePhotoScreen.setFlashMode(AVCaptureFlashMode.Auto, device: device) 

      NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: device) 

      self.session!.addInput(videoDeviceInput) 
      self.videoDeviceInput = videoDeviceInput 

     }else{ 
      self.session!.addInput(self.videoDeviceInput) 
     } 

     self.session!.commitConfiguration() 



     dispatch_async(dispatch_get_main_queue(), { 
    // 
      self.snapButton.enabled = true 
      self.cameraButton.enabled = true 
     }) 

    }) 

} 

我試着用下面的代碼找到一個解決方案,但它在前面的凸輪失敗。你有建議找到解決方法嗎?

let reversedImage = UIImage(CGImage: pickedPhoto!.CGImage!, scale: 1.0, orientation: .LeftMirrored) 

回答

0

我解決了這個錯誤有以下解決方案:

let currentVideoDevice:AVCaptureDevice = self.videoDeviceInput!.device 
let currentPosition: AVCaptureDevicePosition = currentVideoDevice.position    
if currentPosition == AVCaptureDevicePosition.Front 

    {let reversedImage = UIImage(CGImage: image.CGImage!, scale: 1.0, orientation: .LeftMirrored) 
sp.pickedPhoto = reversedImage}