2016-08-02 65 views
2

如何將相機設置爲橫向模式?每次拍照時,圖像都會保存爲肖像圖像。當設備處於橫向模式時,照片看起來很好,但如果我在相機膠捲中看到照片,它仍然是縱向模式。
這是我採取的拍照功能:有沒有辦法將相機快速設置爲橫向模式?

// take a photo 
@IBAction func takePhoto(sender: AnyObject) { 
    self.fullScreenView.hidden = false 
    self.recordButton.enabled = false 
    self.takephoto.enabled = false 
    self.recordButton.hidden = true 
    self.takephoto.hidden = true 

    session.startRunning() 

    // customize the quality level or bitrate of the output photo 
    session.sessionPreset = AVCaptureSessionPresetPhoto 

    // add the AVCaptureVideoPreviewLayer to the view and set the view in fullscreen 
    fullScreenView.frame = view.bounds 
    videoPreviewLayer.frame = fullScreenView.bounds 
    fullScreenView.layer.addSublayer(videoPreviewLayer) 

    // add action to fullScreenView 
    gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:))) 
    self.fullScreenView.addGestureRecognizer(gestureFullScreenView) 

    // add action to myView 
    gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:))) 
    self.view.addGestureRecognizer(gestureView) 

    if (preview == true) { 
     if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) { 
      // code for photo capture goes here... 

      stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in 
       // process the image data (sampleBuffer) here to get an image file we can put in our view 

       if (sampleBuffer != nil) { 
        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
        let image = UIImage(data: imageData, scale: 1.0) 

        self.fullScreenView.hidden = true 
        self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer) 
        self.session.stopRunning() 

        // save image to the library 
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil) 

        self.imageViewBackground = UIImageView(frame: self.view.bounds) 
        self.imageViewBackground.image = image 
        self.imageViewBackground.tag = self.key 

        self.view.addSubview(self.imageViewBackground) 
       } 
      }) 
     } 
    } 
    else { 
     preview = true 
    } 
} 

在此先感謝!

編輯

我的預覽看起來像,這是很正常:

http://img5.fotos-hochladen.net/uploads/bildschirmfotom4s7diaehy.png

但最終它看起來像這樣:

http://img5.fotos-hochladen.net/uploads/bildschirmfoto3c2rlwtevf.png

回答

0

我使用這個函數,wh ich可能對您有用:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
     super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) 

     let orientation = UIDevice.currentDevice().orientation 

     if UIDeviceOrientationIsPortrait(orientation) || UIDeviceOrientationIsLandscape(orientation) { 
      if let videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue) { 
       (captureView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = videoOrientation 
      } 
     } 
    } 
+0

什麼是captureView? – mafioso

+0

它是一個AVCaptureVideoPreviewLayer,它包含連接 – Daniel

+0

captureView.layer不工作 – mafioso

0

set videoOrientation for videoConnection object。

例如爲:

videoConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight

檢查設備的方向,並設置videoOrientation爲縱向和橫向

希望這將解決您的問題開展工作。

相關問題