0
我想構建前置攝像頭,但無法在Swift中構建前置攝像頭,即使我可以構建相機。無法在Swift中使用前置攝像頭
當我只改「回」到「前方」,攝像頭沒有工作......
如何使用前置攝像頭?
var captureSesssion: AVCaptureSession!
var stillImageOutput: AVCapturePhotoOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice!
override func viewDidLoad() {
super.viewDidLoad()
captureSesssion = AVCaptureSession()
stillImageOutput = AVCapturePhotoOutput()
captureSesssion.sessionPreset = AVCaptureSessionPreset1920x1080
captureDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
//When change "front" to "back", camera is built.
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
if (captureSesssion.canAddInput(input)) {
captureSesssion.addInput(input)
if (captureSesssion.canAddOutput(stillImageOutput)) {
captureSesssion.addOutput(stillImageOutput)
captureSesssion.startRunning()
previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
self.view.layer.addSublayer(previewLayer!)
previewLayer?.position = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2)
previewLayer?.bounds = self.view.frame
}
}
}
catch {
print(error)
}
您的測試設備的自拍相機在相機應用程序上工作嗎? – owlswipe
是的,我使用iPhone SE,可以在相機應用程序中使用前置攝像頭。 – bao