2017-06-14 74 views
1

我正在嘗試整合自定義攝像頭視圖,並遵循一些稍微過時的代碼。我有一些錯誤,但我認爲已經解決他們吧2.集成自定義攝像頭視圖AVCaptureDevice

這是迄今爲止當前代碼:

import Foundation 
import AVFoundation 
import UIKit 

class setupView : UIViewController { 

@IBOutlet var cameraView: UIView! 
@IBOutlet var nameTextField: UITextField! 

var captureSession = AVCaptureSession() 
var stillImageOutput = AVCapturePhotoOutput() 
var previewLayer = AVCaptureVideoPreviewLayer() 

override func viewDidLoad() { 

     let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back) 
    if let device = session?.devices[0] { 

     if device.position == AVCaptureDevicePosition.back { 

      do { 

       let input = try AVCaptureDeviceInput(device: device) 
       if captureSession.canAddInput(input){ 
        captureSession.addInput(input) 
        stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG] 

        if captureSession.canAddOutput(stillImageOutput) { 
         captureSession.addOutput(stillImageOutput) 
         captureSession.startRunning() 

         previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
         previewLayer.AVLayerVideoGravityResizeAspectFill 
         previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
         cameraView.layer.addSublayer(previewLayer) 

         previewLayer.bounds = cameraView.frame 
         previewLayer.position = CGPoint(x: cameraView.frame.width/2, y:cameraView.frame.height/2) 

        } 

       } 

      }    catch { 



      } 
     } 
    } 

} 

@IBAction func takePhoto(_ sender: Any) { 
    } 

@IBAction func submitAction(_ sender: Any) { 

} 
} 

目前我得到2個錯誤:

「型AVCapturePhotoOutput的值「不具有成員‘outputSettings’

」的類型‘AVCaptureVideoPreviewLayer’值沒有構件 ‘AVLayerVideoGravityResizeAspectFill’

+0

錯誤「類型AVCapturePhotoOutput價值」很清楚沒有成員「outputSettings」被刪除,如果我恢復到AVCaptureStillImageOutput,但是這已折舊。 –

回答

0
import AVFoundation 
    import Foundation 
    @IBOutlet weak var mainimage: UIImageView! 
    let captureSession = AVCaptureSession() 
    let stillImageOutput = AVCaptureStillImageOutput() 
    var previewLayer : AVCaptureVideoPreviewLayer? 
    var captureDevice : AVCaptureDevice? 
     override func viewDidLoad() { 
     super.viewDidLoad() 
      captureSession.sessionPreset = AVCaptureSessionPresetHigh 

      if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] { 
       // Loop through all the capture devices on this phone 
       for device in devices { 
        // Make sure this particular device supports video 
        if (device.hasMediaType(AVMediaTypeVideo)) { 
         // Finally check the position and confirm we've got the back camera 
         if(device.position == AVCaptureDevicePosition.front) { 
          captureDevice = device 
          if captureDevice != nil { 
           print("Capture device found") 
           beginSession() 
          } 
         } 
        } 
       } 
      } 


    } 



func beginSession() { 

     do { 
      try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice)) 
      stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG] 

      if captureSession.canAddOutput(stillImageOutput) { 
       captureSession.addOutput(stillImageOutput) 
      } 

     } 
     catch { 
      print("error: \(error.localizedDescription)") 
     } 

     guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else { 
      print("no preview layer") 
      return 
     } 

     self.view.layer.addSublayer(previewLayer) 
     previewLayer.frame = self.view.layer.frame 
     captureSession.startRunning() 
     self.view.addSubview(mainimage) 

    } 

此代碼工作在我的應用程序

1

你幾乎沒有。問題是一些AVFoundation類已被棄用,現在有多種方式拍攝照片。這是你的代碼的問題。

「型AVCapturePhotoOutput價值」沒有成員「outputSettings」

這是因爲實際上AVCapturePhotoOutput沒有定義爲outputSettings任何成員。檢查出的AVCapturePhotoOutput

其實outputSettingsdocumentationAVCaptureStillImageOutput成員,同樣是來自iOS的10.0

「價值型 「AVCaptureVideoPreviewLayer棄用」 沒有成員 「AVLayerVideoGravityResizeAspectFill」

同樣的錯誤,根據你的代碼,AVCaptureVideoPreviewLayer沒有成員。如果你想設置視頻預覽層的設置如下。

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 

像你提到的代碼已經過時,其使用已過時AVCaptureStillImageOutput

如果真想用AVCapturePhotoOutput那麼你應該遵循以下步驟。

這些是拍攝照片的步驟。

  • 創建一個AVCapturePhotoOutput對象。使用其屬性確定支持的捕捉設置並啓用某些功能(例如,是否捕捉實時照片)。
  • 創建並配置AVCapturePhotoSettings對象,以便爲特定捕捉(例如,是否啓用圖像穩定或閃光燈)選擇功能和設置。
  • 通過將您的照片設置對象傳遞給capturePhoto(with:delegate :)方法以及實現AVCapturePhotoCaptureDelegate協議的委託對象來捕獲圖像。照片捕捉輸出然後調用您的委託在捕獲過程中通知您重要事件。

有此下面的代碼對你clickCapture方法,不要忘了確認,並實現你的類委託。

let settings = AVCapturePhotoSettings() 
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! 
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType, 
          kCVPixelBufferWidthKey as String: 160, 
          kCVPixelBufferHeightKey as String: 160, 
          ] 
settings.previewPhotoFormat = previewFormat 
self.cameraOutput.capturePhoto(with: settings, delegate: self) 

,如果你想在不同的方式知道從avfoundation拍攝的照片看看我以前的SO answer

也是蘋果文檔解釋爲How to use AVCapturePhotoOutput

相關問題