我正在嘗試整合自定義攝像頭視圖,並遵循一些稍微過時的代碼。我有一些錯誤,但我認爲已經解決他們吧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’
錯誤「類型AVCapturePhotoOutput價值」很清楚沒有成員「outputSettings」被刪除,如果我恢復到AVCaptureStillImageOutput,但是這已折舊。 –