You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 AVCaptureSession was interrupted (e.g., incoming call, background app switch) and never resumed properly, your focus commands will do nothing. Check session.isRunning before attempting to set focus.
  • Stale Camera Reference: If currentCamera isn'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. Verify currentCamera matches the session's input device.

4. iOS Version or Permission Changes

Recent iOS updates sometimes tighten privacy or API behavior:

  • Double-check your Info.plist has a valid NSCameraUsageDescription (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) and currentCamera?.isFocusPointOfInterestSupported right before setting focus—if either returns false, that's your immediate issue.
  • Log the final focusPointOfInterest value to confirm it's within the (0,0) to (1,1) range (required for normalized coordinates).

内容的提问来源于stack exchange,提问作者Mario Kardum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:43:31