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

求助:如何为自定义相机添加人像拍摄模式?AVPhotoCapture开启深度无效果

Hey there! Just turning on image depth in AVPhotoCapture is only the first piece of the puzzle—there are several critical steps you’re probably missing to get that iPhone X-style portrait blur working. Let’s break this down step by step:

Key Steps to Fix Portrait Mode in Your Custom Camera

1. First, Confirm Your Device Supports Portrait Mode

Not all iOS devices can capture portrait effects. You need a device with a dual-camera system (or LiDAR) that supports depth data and portrait matte delivery. Check this upfront with code:

guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { return }
if device.isDepthDataDeliverySupported && device.isPortraitEffectsMatteDeliverySupported {
    // Your device can handle portrait mode
} else {
    // Portrait mode isn't supported here
}

Note: iPhone 7 Plus and later (excluding SE models), iPad Pro 11-inch (2nd gen+) and 12.9-inch (4th gen+) support this.

2. Enable Depth Data Delivery in Your Capture Session

You can’t just toggle depth in capture settings—you need to enable it at the session level first. When configuring your AVCaptureSession, make sure to:

  • Set depthDataDeliveryEnabled to true on your capture device input
  • Select a device format that supports both video and depth data

Here’s a snippet to set this up:

let session = AVCaptureSession()
session.sessionPreset = .photo

guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { return }
do {
    let input = try AVCaptureDeviceInput(device: device)
    if session.canAddInput(input) {
        session.addInput(input)
    }

    // Find a format that supports depth data
    let availableFormats = device.formats.filter { format in
        let supportsDepth = format.isDepthDataDeliverySupported
        let supportsVideo = format.mediaType == .video
        return supportsDepth && supportsVideo
    }
    // Pick the highest resolution format (adjust as needed)
    if let bestFormat = availableFormats.sorted(by: { $0.highResolutionStillImageDimensions.width > $1.highResolutionStillImageDimensions.width }).first {
        try device.lockForConfiguration()
        device.activeFormat = bestFormat
        device.depthDataDeliveryEnabled = true
        device.unlockForConfiguration()
    }
} catch {
    print("Error configuring device: \(error)")
}

3. Configure Capture Settings Properly

When setting up AVPhotoCaptureSettings, don’t forget to enable both depth data and portrait matte delivery—this is what lets you generate the blur effect:

let settings = AVCapturePhotoSettings()
settings.isDepthDataDeliveryEnabled = true
settings.isPortraitEffectsMatteDeliveryEnabled = true
// Prioritize quality to ensure depth data isn't skipped
settings.photoQualityPrioritization = .quality

// Trigger capture
photoOutput.capturePhoto(with: settings, delegate: self)

4. Process the Captured Data to Generate Portrait Blur

The system doesn’t automatically apply the blur for you—you need to use the depth data and portrait matte with Core Image to create the effect. In your AVCapturePhotoOutputDelegate:

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    guard error == nil else {
        print("Capture error: \(error!)")
        return
    }

    // Get the original image, depth data, and portrait matte
    guard let imageData = photo.fileDataRepresentation(),
          let originalImage = UIImage(data: imageData),
          let depthData = photo.depthData,
          let portraitMatte = photo.portraitEffectsMatte else {
        print("Missing required data for portrait effect")
        return
    }

    // Convert to CIImage for processing
    let ciImage = CIImage(image: originalImage)!
    let depthCIImage = depthData.depthMap as CIImage
    let matteCIImage = portraitMatte.mattingImage as CIImage

    // Use CIPortraitEffectFilter to create the blur
    let filter = CIFilter(name: "CIPortraitEffectFilter")!
    filter.setValue(ciImage, forKey: kCIInputImageKey)
    filter.setValue(depthCIImage, forKey: kCIInputDepthDataKey)
    filter.setValue(matteCIImage, forKey: kCIInputPortraitEffectsMatteKey)
    // Adjust blur intensity (0.0 to 1.0)
    filter.setValue(0.7, forKey: kCIInputBlurRadiusKey)

    // Render the filtered image
    guard let filteredImage = filter.outputImage else { return }
    let context = CIContext(options: [.useSoftwareRenderer: false])
    if let cgImage = context.createCGImage(filteredImage, from: filteredImage.extent) {
        let portraitImage = UIImage(cgImage: cgImage)
        // Use portraitImage however you need (display, save, etc.)
    }
}

5. Check Environmental Conditions

Even if all code is correct, portrait mode might fail if:

  • The subject is too close or too far (most devices work best between 2–8 feet)
  • Lighting is too dim (depth sensing relies on good light)
  • The subject isn’t clearly separated from the background
Final Notes

Make sure you’re testing on a physical device (simulators don’t support depth capture) and that you’ve requested the appropriate privacy permissions (NSCameraUsageDescription in Info.plist).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:27:32