获取可以设置的焦距值
let device: AVCaptureDevice = cameraManager.backFacingCamera()
var arrTitleAll: [String] = []
if #available(iOS 13.0, *) {
let arrFactors = device.virtualDeviceSwitchOverVideoZoomFactors
if arrFactors.isEmpty == false {
arrTitleAll.append("0.5x")
for factor in arrFactors {
let trueFactor = factor.floatValue * 0.5
let strFactor = NSString(format: "%@", NSNumber(value: trueFactor)) as String
let title = strFactor as String + "x"
arrTitleAll.append(title)
}
} else {
arrTitleAll = ["0.5x", "1x"]
}
} else {
arrTitleAll = ["0.5x", "1x"]
}
arrBtnFocusTitleAll = arrTitleAll
btnFocalLength.tag = 1
changeCameraZoom()
设置焦距
func changeCameraZoom() {
//调整缩放因子
let floatValue = ((btnFocalLength.currentTitle ?? "") as NSString).floatValue
var factor = Float(floatValue)
factor = factor >= 1 ? (factor * 2) : factor
cameraManager.setZoom(CGFloat(factor))
print(CGFloat(factor))
}
获取当前设备(这部分为就代码,为Objective-C实现)
// 根据位置查找摄像头
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
if (@available(iOS 13.0, *)) {
AVCaptureDeviceDiscoverySession * session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInTripleCamera, AVCaptureDeviceTypeBuiltInTrueDepthCamera, AVCaptureDeviceTypeBuiltInDualWideCamera, AVCaptureDeviceTypeBuiltInDualCamera, AVCaptureDeviceTypeBuiltInUltraWideCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
// AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInTripleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
NSArray *devices = session.devices;
AVCaptureDevice *deviceResult = nil;
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
deviceResult = device;
break;
}
}
return deviceResult;
} else {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
}
return nil;
}
网友评论