Ios AVFoundation videoZoomFactor不工作(Swift)
除了zoomfactor,我想使用的所有相机调整都正常工作。我不明白为什么会这样……有什么想法吗?自定义曝光和焦点设置工作正常。iOS中有什么我不知道的变化吗Ios AVFoundation videoZoomFactor不工作(Swift),ios,swift,avfoundation,Ios,Swift,Avfoundation,除了zoomfactor,我想使用的所有相机调整都正常工作。我不明白为什么会这样……有什么想法吗?自定义曝光和焦点设置工作正常。iOS中有什么我不知道的变化吗 captureSession = AVCaptureSession() captureSession?.sessionPreset = AVCaptureSessionPresetPhoto stillImageOutput = AVCapturePhotoOutput() let device = AVCapt
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPresetPhoto
stillImageOutput = AVCapturePhotoOutput()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do{
do{
try device?.lockForConfiguration()
device?.setFocusModeLockedWithLensPosition(focusValue, completionHandler: {(time) -> Void in})
device?.setExposureModeCustomWithDuration(CMTimeMake(1, exposureValue), iso: ISOValue, completionHandler: {(time) -> Void in})
let zoomFactor:CGFloat = 16
device?.videoZoomFactor = zoomFactor
device?.unlockForConfiguration()
}catch{
print(error)
}
stillImageOutput.isHighResolutionCaptureEnabled = true
let input = try AVCaptureDeviceInput(device: device)
if(captureSession.canAddInput(input)){
captureSession.addInput(input)
if(captureSession.canAddOutput(stillImageOutput)){
captureSession.addOutput(stillImageOutput)
captureSession.startRunning()
let captureVideoLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer.init(session: captureSession)
captureVideoLayer.frame = self.previewView.bounds
captureVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.previewView.layer.insertSublayer(captureVideoLayer, at: 0)
}
}
}catch{
print(error)
}
原来我只是在错误的地方设置了我的设备设置。如果将lockconfig try块移到捕获会话内容下方,它就会按预期工作。使用苹果的照片和视频采集示例代码,即AVFoundation,我尝试在代码中更改iPhone摄像头的设备缩放 在的帮助下,我意识到放置缩放操作属性的代码位置很重要,而且,在操作任何视频设备之前,请确保使用device.lockForConfiguration!两者都是重要的代码位置和锁定 这里有一个链接和一个屏幕截图,可以看到下载按钮,原始的苹果示例可以从中获取: 以下是原始Apple示例的代码摘录,其中包含我的代码,用于操纵插入到正确位置的缩放: Swift-4.2/Xcode 10.0,iOS 11.0 SDK
// Call this on the session queue.
private func configureSession() {
if setupResult != .success {
return
}
// ... missing original code (not important for this illustration)...
// Add video input.
do {
var defaultVideoDevice: AVCaptureDevice?
// ... missing original code (not important for this illustration)...
if session.canAddInput(videoDeviceInput) {
session.addInput(videoDeviceInput)
self.videoDeviceInput = videoDeviceInput
// ... missing original code (not important for this illustration)...
} else {
print("Could not add video device input to the session")
setupResult = .configurationFailed
session.commitConfiguration()
return
}
// !!!!!!!!!!!!!!!! MY CODE THAT MANIPULATES THE ZOOM !!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!! PLACE IT HERE AND ZOOM WILL WORK !!!!!!!!!!!!!!!!!!!!!!!!!!!!
guard let device = defaultVideoDevice else { return }
do {
try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = 10.0
} catch {
debugPrint(error)
}
// !!!!!!!!!!!!!! END OF MY CODE THAT MANIPULATES THE ZOOM !!!!!!!!!!!!!!!!!!!!!!!!!!
} catch {
print("Could not create video device input: \(error)")
setupResult = .configurationFailed
session.commitConfiguration()
return
}
// Add audio input.
do {
let audioDevice = AVCaptureDevice.default(for: .audio)
let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice!)
if session.canAddInput(audioDeviceInput) {
session.addInput(audioDeviceInput)
} else {
print("Could not add audio device input to the session")
}
} catch {
print("Could not create audio device input: \(error)")
}
// Add photo output.
if session.canAddOutput(photoOutput) {
session.addOutput(photoOutput)
photoOutput.isHighResolutionCaptureEnabled = true
photoOutput.isLivePhotoCaptureEnabled = photoOutput.isLivePhotoCaptureSupported
photoOutput.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliverySupported
livePhotoMode = photoOutput.isLivePhotoCaptureSupported ? .on : .off
depthDataDeliveryMode = photoOutput.isDepthDataDeliverySupported ? .on : .off
} else {
print("Could not add photo output to the session")
setupResult = .configurationFailed
session.commitConfiguration()
return
}
session.commitConfiguration()
}
我仍然认为videozoomfactor在ios11中被破坏了。有人能证实吗?是的,就是这样!非常感谢你!请参阅下面回答中我的videoZoomFactor代码的位置,以获取Swift-4.2/Xcode 9.0、iOS 11.0 SDK