Skip to content

Commit a2ae2fe

Browse files
authored
Merge pull request #2 from crontab/square-only
Demp app: removed the non-square viewfinder shapes for simplicity
2 parents 6b7ed98 + d7567c9 commit a2ae2fe

File tree

3 files changed

+20
-51
lines changed

3 files changed

+20
-51
lines changed

PhotoCameraDemo/Sources/CameraView/CameraView.swift

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@ import UIKit.UIImage
1010
import PhotoCamera
1111
import PhotosUI
1212

13-
enum ViewfinderShape {
14-
case round
15-
case square
16-
case rect3x4
17-
case rect9x16
18-
19-
var ratio: CGFloat {
20-
switch self {
21-
case .round, .square: 1
22-
case .rect3x4: 3.0 / 4
23-
case .rect9x16: 9.0 / 16
24-
}
25-
}
26-
}
27-
2813
private let largeButtonSize = CGSize(width: 64, height: 64)
2914
private let toolbarHeight = 88.0
3015
private let maxToolbarWidth = 360.0
@@ -34,7 +19,7 @@ struct CameraView: View {
3419

3520
@Environment(\.dismiss) private var dismiss
3621

37-
let viewfinderShape: ViewfinderShape
22+
let isRound: Bool
3823
let onConfirm: (UIImage?) -> Void
3924

4025
@State private var camera: Camera
@@ -43,9 +28,9 @@ struct CameraView: View {
4328
@State private var capturedImage: UIImage? // result
4429
@State private var libraryItem: PhotosPickerItem?
4530

46-
init(forSelfie: Bool, viewfinderShape: ViewfinderShape, onConfirm: @escaping (UIImage?) -> Void) {
31+
init(forSelfie: Bool, isRound: Bool, onConfirm: @escaping (UIImage?) -> Void) {
4732
self.camera = Camera(forSelfie: forSelfie)
48-
self.viewfinderShape = viewfinderShape
33+
self.isRound = isRound
4934
self.onConfirm = onConfirm
5035
}
5136

@@ -132,32 +117,27 @@ struct CameraView: View {
132117

133118
private func viewfinderContainer(viewSize: CGSize, @ViewBuilder content: @escaping () -> some View) -> some View {
134119
VStack {
135-
let ratio = viewfinderShape.ratio
120+
let ratio = 1.0
136121
let viewRatio = viewSize.width / viewSize.height
137122
let landscape = viewRatio > ratio
138-
let is916 = viewfinderShape == .rect9x16
139-
let pad = is916 ? 0 : 16.0
123+
let pad = 16.0
140124
let width = max(0, (landscape ? viewSize.height * ratio : viewSize.width) - pad * 2)
141125
let height = max(0, (landscape ? viewSize.height : viewSize.width / ratio) - pad * 2)
142-
if viewfinderShape != .rect9x16 {
143-
Spacer()
144-
}
126+
Spacer()
145127
content()
146128
.aspectRatio(ratio, contentMode: .fill)
147129
.frame(width: width, height: height)
148130
.blur(radius: blurRadius, opaque: true)
149131
.overlay {
150-
if viewfinderShape == .round {
132+
if isRound {
151133
holeMask(width: width, height: height)
152134
.allowsHitTesting(false) // allow user-initiated focus/exposure taps
153135
}
154136
}
155137
.clipped()
156138
.offset(y: viewfinderYOffset(landscape: landscape))
157139
.onChange(of: camera.isSwitchingVideoDevices, updateBlurRadius(_:_:))
158-
if viewfinderShape != .rect9x16 {
159-
Spacer()
160-
}
140+
Spacer()
161141
}
162142
}
163143

@@ -178,7 +158,7 @@ struct CameraView: View {
178158

179159
private func viewfinderYOffset(landscape: Bool) -> CGFloat {
180160
// Move smaller viewfinders up a little bit, only in portrait mode
181-
!landscape && [.round, .square, .rect3x4].contains(viewfinderShape) ? -80.0 / 2 : 0
161+
!landscape ? -80.0 / 2 : 0
182162
}
183163

184164
private func updateBlurRadius(_: Bool, _ isSwitching: Bool) {
@@ -191,8 +171,9 @@ struct CameraView: View {
191171

192172
private func cameraUI() -> some View {
193173
GeometryReader { proxy in
174+
let ratio = 1.0
194175
let viewRatio = proxy.size.width / proxy.size.height
195-
let landscape = viewRatio > viewfinderShape.ratio
176+
let landscape = viewRatio > ratio
196177
stack(vertical: !landscape) {
197178
Spacer()
198179
stack(vertical: landscape) {
@@ -240,7 +221,7 @@ struct CameraView: View {
240221
private func confirmButton() -> some View {
241222
Button {
242223
dismiss()
243-
onConfirm(capturedImage?.cropped(ratio: viewfinderShape.ratio))
224+
onConfirm(capturedImage?.cropped(ratio: 1))
244225
} label: {
245226
Image(systemName: "checkmark")
246227
}
@@ -336,17 +317,9 @@ private extension View {
336317
// MARK: - Previews
337318

338319
#Preview("Round") {
339-
CameraView(forSelfie: true, viewfinderShape: .round) { _ in }
320+
CameraView(forSelfie: true, isRound: true) { _ in }
340321
}
341322

342323
#Preview("Square") {
343-
CameraView(forSelfie: true, viewfinderShape: .square) { _ in }
344-
}
345-
346-
#Preview("Rect3x4") {
347-
CameraView(forSelfie: true, viewfinderShape: .rect3x4) { _ in }
348-
}
349-
350-
#Preview("Rect9x16") {
351-
CameraView(forSelfie: true, viewfinderShape: .rect9x16) { _ in }
324+
CameraView(forSelfie: true, isRound: false) { _ in }
352325
}

PhotoCameraDemo/Sources/MainDemoView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct MainDemoView: View {
4444
.padding(.bottom)
4545
}
4646
.fullScreenCover(isPresented: $isCameraPresented) {
47-
CameraView(forSelfie: true, viewfinderShape: .round) { image in
47+
CameraView(forSelfie: true, isRound: true) { image in
4848
capturedImage = image?.fitted(maxWidth: 500)
4949
}
5050
}

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ The non-UI modules are separated into a framework which you can link to your pro
88

99
The demo app shows how to build a camera view that uses the framework. You can copy the demo app modules to your project and customize them to your needs.
1010

11-
The demo view (`CameraView`) supports several shapes for the viewfinder: round, square, 3x4, and 9x16:
11+
The demo view (`CameraView`) supports two shapes for the viewfinder, round and square:
1212

13-
![IMG_0E4B03DB4266-1](https://github.com/user-attachments/assets/10a23bc3-b318-4b5d-a41b-587f306ac71e) ![IMG_FE9A046A87BE-1](https://github.com/user-attachments/assets/a0ac58d5-65e2-45b9-97f3-d85bf10e8538) ![IMG_680C51161968-1](https://github.com/user-attachments/assets/612bd08d-102a-4234-afc1-d12076db9c96) ![IMG_48CB940B0343-1](https://github.com/user-attachments/assets/adac17e5-c1b3-4d22-a4d1-811204fb8761)
14-
15-
Landscape mode is also supported: the view adjusts itself by fitting the buttons on the right and extending the viewfinder to fill the full height of the screen:
16-
17-
![Simulator Screenshot - iPad mini (A17 Pro) - 2025-01-18 at 20 29 33](https://github.com/user-attachments/assets/7a51303c-40e4-4e1e-9a79-301dbbd9c6d3)
18-
19-
The resuling image can be cropped and resizied using the utility methods in `UIImageCameraEx.swift`.
13+
![IMG_0E4B03DB4266-1](https://github.com/user-attachments/assets/10a23bc3-b318-4b5d-a41b-587f306ac71e) ![IMG_FE9A046A87BE-1](https://github.com/user-attachments/assets/a0ac58d5-65e2-45b9-97f3-d85bf10e8538)
2014

2115
Importing from the photo library is also supported.
2216

17+
The resuling image can be cropped and resizied programmatically using the utility methods in the demo app's `UIImageCameraEx.swift`.
18+
2319
## Authors
2420

25-
Hovik Melikyan, https://github.com/crontab
21+
[Hovik Melikyan](https://github.com/crontab)

0 commit comments

Comments
 (0)