Skip to content

Commit a55096f

Browse files
authored
fix: Remove preview layers as well in updateOutputs(..) (#4134)
* fix: Remove preview layers as well in `updateOutputs(..)` * Update HybridCameraSession.swift * fix: Do it before updateInputs/updateOutputs * remove -> detach * Update HybridCameraSession.swift
1 parent ff70e80 commit a55096f

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

packages/react-native-vision-camera/ios/Hybrid Objects/HybridCameraSession.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ final class HybridCameraSession: HybridCameraSessionSpec {
6666
)
6767
}
6868

69+
// Detach all unwanted preview layers before touching inputs/outputs
70+
self.detachUnwantedPreviewLayers(connections)
6971
// Remove all unwanted inputs and add all new inputs
7072
try self.updateInputs(connections)
7173
// Remove all unwanted outputs and add all new outputs
@@ -228,6 +230,25 @@ final class HybridCameraSession: HybridCameraSessionSpec {
228230
}
229231

230232
// pragma MARK: Helpers
233+
/**
234+
* Detach all preview layers that are not listed in the [targetConnections] array
235+
* from this [AVCaptureSession].
236+
* This must run before [updateInputs] or [updateOutputs], as those methods kill
237+
* preview connections without unsetting the `session`.
238+
*/
239+
private func detachUnwantedPreviewLayers(_ targetConnections: [ResolvedCameraSessionConnection]) {
240+
let currentlyAttachedPreviewLayers = self.session.connections.compactMap { $0.videoPreviewLayer }
241+
for currentlyAttachedPreviewLayer in currentlyAttachedPreviewLayers {
242+
let containsAttachedPreviewLayer = targetConnections.contains { connection in
243+
return connection.isConnectedTo(preview: currentlyAttachedPreviewLayer)
244+
}
245+
if !containsAttachedPreviewLayer {
246+
logger.info("Removing preview \(currentlyAttachedPreviewLayer)...")
247+
currentlyAttachedPreviewLayer.session = nil
248+
}
249+
}
250+
}
251+
231252
/**
232253
* Adds all inputs on the given [targetConnections] if they haven't been added yet,
233254
* and removes all current inputs that aren't listed in the [connections] array.
@@ -289,6 +310,7 @@ final class HybridCameraSession: HybridCameraSessionSpec {
289310
}
290311
}
291312
}
313+
292314
/**
293315
* Adds all outputs on the given [targetConnections] if they haven't been added yet,
294316
* and removes all current outputs that aren't listed in the [targetConnections] array.

0 commit comments

Comments
 (0)