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

SwiftPM格式Xcode App Playground中Vision框架触发EXC_BREAKPOINT错误

Swift Playground(SwiftPM格式)中Vision框架面部检测崩溃问题解决

问题描述

在SwiftPM格式的App Playground中使用Vision框架执行面部检测时,调用try handler.perform([request])触发Thread 12: EXC_BREAKPOINT (code=1, subcode=0x10321c2a8)错误,但相同代码在常规Xcode项目(.xcodeproj)中可正常运行。图片已放置在.xcassets文件夹中。

解决方案

1. 配置SwiftPM资源声明

确保Package.swift中正确配置资源处理,让SwiftPM识别你的xcassets资源:

let package = Package(
    name: "YourPlaygroundApp",
    platforms: [.iOS(.v16)], // 根据目标平台调整版本
    products: [
        .application(name: "YourPlaygroundApp", targets: ["YourPlaygroundApp"])
    ],
    dependencies: [],
    targets: [
        .target(
            name: "YourPlaygroundApp",
            dependencies: [],
            resources: [
                .process("Assets.xcassets") // 指向你的xcassets文件路径
            ]
        )
    ]
)

2. 修正SwiftPM下的图片加载逻辑

SwiftPM项目中,资源属于模块专属Bundle,不能直接通过UIImage(named:)加载,需指定Bundle.module:

guard let image = UIImage(named: "me", in: Bundle.module, with: nil),
      let cgImage = image.cgImage else {
    print("图片加载失败")
    return
}

3. 将Vision请求移至后台线程执行

Vision框架的请求执行不适合在UI线程进行,Playground的UI线程有执行限制,将检测逻辑放到后台队列:
修改onAppear中的调用:

.onAppear {
    DispatchQueue.global(qos: .userInitiated).async {
        detectFace()
    }
}

若后续需要在请求回调中更新UI,需切回主线程:

let request = VNDetectFaceRectanglesRequest { request, error in
    DispatchQueue.main.async {
        // 此处处理UI更新操作
    }
    // 原有打印逻辑保持不变
}

完整修正代码

import SwiftUI
import Vision

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Face Detection")
                .font(.largeTitle)
                .padding()
            
            Image("me")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onAppear {
                    DispatchQueue.global(qos: .userInitiated).async {
                        detectFace()
                    }
                }
        }
    }
    
    func detectFace() {
        guard let image = UIImage(named: "me", in: Bundle.module, with: nil),
              let cgImage = image.cgImage else {
            print("Failed to load image from bundle")
            return
        }

        let request = VNDetectFaceRectanglesRequest { request, error in
            if let results = request.results as? [VNFaceObservation] {
                print("Detected \(results.count) face(s).")
                for face in results {
                    print("Bounding Box: \(face.boundingBox)")
                }
            } else {
                print("No faces detected.")
            }
        }
        
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        do {
            try handler.perform([request])
        } catch {
            print("Failed to perform Vision request: \(error)")
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 10:59:56