Skip to content

Commit 6f2374f

Browse files
authored
fix: Fix Swift 6 compiler crash in HybridBarcodeScannerOutput.swift (#3832)
Fix Swift 6 / Xcode 26 compile crash in HybridBarcodeScannerOutput.swift. Co-authored-by: chenxiang <>
1 parent 174d2ed commit 6f2374f

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

packages/react-native-vision-camera-barcode-scanner/ios/HybridBarcodeScannerOutput.swift

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import VisionCamera
1313

1414
final class HybridBarcodeScannerOutput: HybridCameraOutputSpec, NativeCameraOutput {
1515
private let scanner: BarcodeScanner
16+
private let onBarcodeScanned: (_ barcodes: [any HybridBarcodeSpec]) -> Void
17+
private let onError: (_ error: Error) -> Void
18+
private var isScanning = false
1619
private var delegate: BarcodeScannerDelegate? = nil
1720
private let queue: DispatchQueue
1821
let output: AVCaptureVideoDataOutput
@@ -32,37 +35,15 @@ final class HybridBarcodeScannerOutput: HybridCameraOutputSpec, NativeCameraOutp
3235

3336
init(options: BarcodeScannerOutputOptions) {
3437
self.scanner = BarcodeScanner.barcodeScanner(options: options.toMLKitOptions())
38+
self.onBarcodeScanned = options.onBarcodeScanned
39+
self.onError = options.onError
3540
self.queue = DispatchQueue(label: "com.margelo.camera.barcodescanner")
3641
self.output = AVCaptureVideoDataOutput()
3742
super.init()
3843

3944
// set delegate
40-
var isScanning = false
4145
self.delegate = BarcodeScannerDelegate(onSampleBuffer: { [weak self] buffer in
42-
guard let self else { return }
43-
if isScanning { return }
44-
45-
// prepare MLImage
46-
isScanning = true
47-
guard let image = MLImage(sampleBuffer: buffer) else {
48-
options.onError(
49-
RuntimeError.error(withMessage: "Failed to convert CMSampleBuffer to MLImage!"))
50-
return
51-
}
52-
image.orientation = self.outputOrientation.toUIImageOrientation()
53-
// start scanning
54-
self.scanner.process(image) { barcodes, error in
55-
isScanning = false
56-
if let barcodes {
57-
// scanned x barcodes!
58-
let hybridBarcodes = barcodes.map { HybridBarcode(barcode: $0) }
59-
options.onBarcodeScanned(hybridBarcodes)
60-
}
61-
if let error {
62-
// error
63-
options.onError(error)
64-
}
65-
}
46+
self?.scan(buffer)
6647
})
6748
self.output.setSampleBufferDelegate(delegate, queue: queue)
6849
self.output.alwaysDiscardsLateVideoFrames = true
@@ -72,6 +53,33 @@ final class HybridBarcodeScannerOutput: HybridCameraOutputSpec, NativeCameraOutp
7253
}
7354
}
7455

56+
private func scan(_ buffer: CMSampleBuffer) {
57+
if isScanning { return }
58+
59+
// prepare MLImage
60+
isScanning = true
61+
guard let image = MLImage(sampleBuffer: buffer) else {
62+
isScanning = false
63+
onError(RuntimeError.error(withMessage: "Failed to convert CMSampleBuffer to MLImage!"))
64+
return
65+
}
66+
image.orientation = outputOrientation.toUIImageOrientation()
67+
// start scanning
68+
scanner.process(image) { [weak self] barcodes, error in
69+
guard let self else { return }
70+
self.isScanning = false
71+
if let barcodes {
72+
// scanned x barcodes!
73+
let hybridBarcodes: [any HybridBarcodeSpec] = barcodes.map { HybridBarcode(barcode: $0) }
74+
self.onBarcodeScanned(hybridBarcodes)
75+
}
76+
if let error {
77+
// error
78+
self.onError(error)
79+
}
80+
}
81+
}
82+
7583
func configure(config: CameraOutputConfiguration) {
7684
guard let connection = self.output.connection(with: .video) else {
7785
return

0 commit comments

Comments
 (0)