AVCapture手动聚焦代码突然失效求助:原可用代码无法工作
Hey there, sorry to hear your manual focus code stopped working out of nowhere—let's break down the most likely culprits based on your code snippet and common AVCapture pitfalls:
1. Coordinate Calculation Mismatch
Looking at your code, you're normalizing the tap point using view.frame.size.width/height, but the tap location is relative to photoImage (your preview container). If photoImage's bounds don't match the main view's frame (e.g., due to auto layout, screen rotation, or aspect ratio differences), this will throw off the focus point entirely.
Fix this by using photoImage's bounds instead:
let focus_x = thisFocusPoint.x / photoImage.bounds.width let focus_y = thisFocusPoint.y / photoImage.bounds.height
Even better, use the AVCapturePreviewLayer's built-in conversion method to handle any video gravity settings (like .resizeAspectFill that crops the preview):
guard let previewLayer = photoImage.layer as? AVCapturePreviewLayer else { return } let convertedPoint = previewLayer.captureDevicePointConverted(fromLayerPoint: thisFocusPoint) // Use convertedPoint directly as focusPointOfInterest (it's already normalized!)
2. Missing Device Configuration Lock
AVCaptureDevice settings (like focus mode) require you to lock the device before modifying them. If your code omitted this step (or it got accidentally removed), the changes won't take effect at all. Here's the correct, safe flow:
do { try currentCamera?.lockForConfiguration() guard currentCamera!.isFocusModeSupported(.autoFocus), currentCamera!.isFocusPointOfInterestSupported else { currentCamera?.unlockForConfiguration() return } currentCamera!.focusPointOfInterest = convertedPoint // Or your calculated CGPoint currentCamera!.focusMode = .autoFocus // Optional: Sync exposure with the focus point for better results currentCamera!.exposurePointOfInterest = convertedPoint currentCamera!.exposureMode = .autoExpose currentCamera?.unlockForConfiguration() } catch { print("Camera config lock failed: \(error.localizedDescription)") }
Add that error print to catch silent failures you might be missing right now.
3. Camera Session State or Reference Issues
- Session Not Running: If the
AVCaptureSessionwas interrupted (e.g., incoming call, background app switch) and never resumed properly, your focus commands will do nothing. Checksession.isRunningbefore attempting to set focus. - Stale Camera Reference: If
currentCameraisn't pointing to the active camera (e.g., you switched front/back but didn't update the variable), you might be trying to configure a camera that's not actually in use. VerifycurrentCameramatches the session's input device.
4. iOS Version or Permission Changes
Recent iOS updates sometimes tighten privacy or API behavior:
- Double-check your
Info.plisthas a validNSCameraUsageDescription(required for iOS 10+). If this was removed or invalidated, camera access might be restricted silently. - On iOS 14+, ensure you're not trying to access the camera while the app is in the background (unless you've enabled the appropriate background modes).
Quick Debug Tips
- Print
currentCamera?.isFocusModeSupported(.autoFocus)andcurrentCamera?.isFocusPointOfInterestSupportedright before setting focus—if either returnsfalse, that's your immediate issue. - Log the final
focusPointOfInterestvalue to confirm it's within the(0,0)to(1,1)range (required for normalized coordinates).
内容的提问来源于stack exchange,提问作者Mario Kardum

