Skip to content

Commit c367370

Browse files
committed
Demo: show the preview as an overlay while video is still running.
This is a fix for macOS: hiding and showing the video layer without restarting the session is problematic there.
1 parent fa0010c commit c367370

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

PhotoCameraDemo/PhotoCameraDemo/Sources/CameraView/CameraView.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,29 @@ struct CameraView: View {
3939
GeometryReader { proxy in
4040
// A container view that manages the placement of the preview.
4141
viewfinderContainer(viewSize: proxy.size) {
42-
// A view that provides a preview of the captured content.
43-
if let capturedImage {
44-
Image(uiImage: capturedImage)
45-
.resizable()
46-
.aspectRatio(contentMode: .fill)
47-
}
48-
else {
49-
ViewfinderView(camera: camera)
50-
// Handle capture events from device hardware buttons.
51-
.onCameraCaptureEvent { event in
52-
if event.phase == .ended {
53-
capturePhoto()
54-
}
42+
ViewfinderView(camera: camera)
43+
// Handle capture events from device hardware buttons.
44+
.onCameraCaptureEvent { event in
45+
if event.phase == .ended {
46+
capturePhoto()
5547
}
48+
}
5649

57-
// Focus and expose at the tapped point.
58-
.onTapGesture { location in
59-
Task { await camera.focusAndExpose(at: location) }
50+
// Focus and expose at the tapped point.
51+
.onTapGesture { location in
52+
Task { await camera.focusAndExpose(at: location) }
53+
}
54+
.opacity(blink ? 0 : 1)
55+
56+
// A view that provides a preview of the captured content.
57+
.overlay {
58+
// This is done as an overlay because hiding and showing the video layer (ViewfinderView above) without restarting the session causes strange problems on macOS, though is fine on the iPhone.
59+
if let capturedImage {
60+
Image(uiImage: capturedImage)
61+
.resizable()
62+
.aspectRatio(contentMode: .fill)
6063
}
61-
.opacity(blink ? 0 : 1)
62-
}
64+
}
6365
}
6466
.frame(maxWidth: .infinity)
6567

0 commit comments

Comments
 (0)