iOS自动变焦

463 阅读1分钟

背景

随着iOS设备摄像头的不断加多增强,在开发过程中自定义相机时需要自动变焦(参考微信相机)

核心技术实现(摄像头选择)

//在不同场景下选择合适的摄像头即可
let device = AVCaptureDevice.default(for: AVMediaType.video)
var types: [AVCaptureDevice.DeviceType] = [.builtInWideAngleCamera]
 if #available(iOS 13.0, *) { iOS
    types.insert(.builtInDualWideCamera, at: 0) //双广角相机
     types.insert(.builtInUltraWideCamera, at: 0) //超广角摄像头
 }
 //设备支持的最清晰摄像头
 for type in types {
     if let aDevice = AVCaptureDevice.default(type, for: AVMediaType.video, position: AVCaptureDevice.Position.back), !aDevice.localizedName.isEmpty {
     device = aDevice
     break
    }
  }