2016-10-20 163 views
11

我正在通過此視頻觀看以製作自定義相機視圖。 https://www.youtube.com/watch?v=w0O3ZGUS3pkSwift 3 - AVCapture自定義相機視圖

但是由於iOS的10和3迅速改變很多事情都不再相關

以下是我改變棄用功能新ones..however沒有錯誤後,下了代碼,也沒有看到對UIView的預覽

import UIKit 
import AVFoundation 

class ViewController: UIViewController, AVCapturePhotoCaptureDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    @IBOutlet weak var cameraView: UIView! 
    var captureSession = AVCaptureSession(); 
    var sessionOutput = AVCapturePhotoOutput(); 
    var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG]); 
    var previewLayer = AVCaptureVideoPreviewLayer(); 

    override func viewWillAppear(_ animated: Bool) { 
     let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified) 
     for device in (deviceDiscoverySession?.devices)! { 
      if(device.position == AVCaptureDevicePosition.front){ 
       do{ 
        let input = try AVCaptureDeviceInput(device: device) 
        if(captureSession.canAddInput(input)){ 
         captureSession.addInput(input); 

         if(captureSession.canAddOutput(sessionOutput)){ 
          captureSession.addOutput(sessionOutput); 
          previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); 
          previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
          previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait; 
          cameraView.layer.addSublayer(previewLayer); 
         } 
        } 
       } 
       catch{ 
        print("exception!"); 
       } 
      } 
     } 
    } 

回答

15

你忘了啓動會話:

captureSession.startRunning() 

,併爲您的previewLayer框架:

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    previewLayer.frame = cameraView.bounds 
} 

附:一旦你添加了會話輸入和輸出,你應該跳出這個循環。

+0

它應該只運行一次而不管? –

+1

可能是這樣,但它讓我很緊張 –

+1

例如,iPhone 7+上沒有兩個背面攝像頭嗎?這是不太可能的,但是你可能會在未來的硬件上遇到一個令人討厭的驚喜... –

相關問題