回傳上級目錄:ios AVFoundation(音視頻解碼,直播,相機)
文章目錄
- 官方檔案
- 示例代碼:
- AVCaptureDevice.default方法三個引數的解釋
官方檔案
蘋果官方檔案:Setting Up a Capture Session
示例代碼:
import UIKit
import AVFoundation
class PreviewView: UIView {
override class var layerClass: AnyClass {
return AVCaptureVideoPreviewLayer.self
}
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return layer as! AVCaptureVideoPreviewLayer
}
}
class ViewController: UIViewController {
var captureSession: AVCaptureSession?
let previewView = PreviewView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized: // The user has previously granted access to the camera.
self.setupCaptureSession()
case .notDetermined: // The user has not yet been asked for camera access.
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.setupCaptureSession()
}
}
case .denied: // The user has previously denied access.
return
case .restricted: // The user can't grant access due to restrictions.
return
}
}
func setupCaptureSession() {
captureSession = AVCaptureSession()
guard let captureSession = captureSession else {
return
}
captureSession.beginConfiguration()
let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
guard let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice!),captureSession.canAddInput(videoDeviceInput) else {
return
}
captureSession.addInput(videoDeviceInput)
let photoOutput = AVCapturePhotoOutput()
guard captureSession.canAddOutput(photoOutput) else { return }
captureSession.sessionPreset = .photo
captureSession.addOutput(photoOutput)
captureSession.commitConfiguration()
previewView.frame = view.bounds
previewView.videoPreviewLayer.session = captureSession
view.addSubview(previewView)
captureSession.startRunning()
}
}
AVCaptureDevice.default方法三個引數的解釋
引數的列舉選擇不對,可能會導致初始化失敗,如設備型別你選了.builtInDualWideCamera.但是6s的手機沒有兩個廣角相機,那么在6s手機上運行是就會回傳nil

設備型別 AVCaptureDevice.DeviceType


參考:
OC之AVCaptureDevice
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/68882.html
標籤:其他
上一篇:微信小程式 - 藍牙列印機、支持賬單模式、標簽模式、查詢列印機狀態、分包發送
下一篇:關于RFID功能的開發記錄
