Skip to content

Commit 024ebee

Browse files
authored
Patch 2.1.1
fix: - Fixed a problem with the tap and zoom gestures not working in release build configuration (#52) - Fixed an issue with icons not loading in the latest iOS version (#48) - Fixed an issue with camera output rotation when camera rotation is locked (#40)
1 parent 0ff7c42 commit 024ebee

File tree

5 files changed

+87
-76
lines changed

5 files changed

+87
-76
lines changed

Sources/Internal/Extensions/AVCaptureVideoOrientation++.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ extension AVCaptureVideoOrientation {
3333
default: .up
3434
}}
3535
}
36+
37+
// MARK: - To UIDeviceOrientation
38+
extension AVCaptureVideoOrientation {
39+
func toDeviceOrientation() -> UIDeviceOrientation { switch self {
40+
case .portrait: .portrait
41+
case .portraitUpsideDown: .portraitUpsideDown
42+
case .landscapeLeft: .landscapeLeft
43+
case .landscapeRight: .landscapeRight
44+
default: .portrait
45+
}}
46+
}

Sources/Internal/Extensions/Bundle++.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
import Foundation
1313

1414
extension Bundle {
15-
static var mijick: Bundle { .init(identifier: "MijickCameraView-MijickCameraView-resources") ?? .main }
15+
static var mijick: Bundle {
16+
.allBundles
17+
.compactMap { $0.resourceURL?.appendingPathComponent("MijickCameraView_MijickCameraView", isDirectory: false).appendingPathExtension("bundle") }
18+
.compactMap { Bundle(url: $0) }
19+
.first ?? .main
20+
}
1621
}

Sources/Internal/Managers/CameraManager.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private extension CameraManager {
117117
cameraMetalView = nil
118118
cameraGridView = nil
119119
cameraBlurView = nil
120-
cameraFocusView = .init()
120+
cameraFocusView = .create(image: .iconCrosshair, tintColor: .yellow, size: 92)
121121
motionManager = .init()
122122
}
123123
func removeObservers() {
@@ -305,18 +305,6 @@ extension CameraManager {
305305
}
306306
}
307307

308-
// MARK: - Camera Rotation
309-
extension CameraManager {
310-
func fixCameraRotation() { if !orientationLocked {
311-
redrawGrid()
312-
}}
313-
}
314-
private extension CameraManager {
315-
func redrawGrid() {
316-
cameraGridView?.draw(.zero)
317-
}
318-
}
319-
320308
// MARK: - Changing Output Type
321309
extension CameraManager {
322310
func changeOutputType(_ newOutputType: CameraOutputType) throws { if newOutputType != attributes.outputType && !isChanging {
@@ -398,7 +386,7 @@ private extension CameraManager {
398386
animateCameraFocusView()
399387
}}
400388
func setCameraFocus(_ touchPoint: CGPoint, _ device: AVCaptureDevice) throws {
401-
let focusPoint = cameraLayer.captureDevicePointConverted(fromLayerPoint: touchPoint)
389+
let focusPoint = convertTouchPointToFocusPoint(touchPoint)
402390
try configureCameraFocus(focusPoint, device)
403391
}
404392
}
@@ -417,6 +405,10 @@ private extension CameraManager {
417405
UIView.animate(withDuration: 0.5, delay: 3.5) { [self] in cameraFocusView.alpha = 0 }
418406
}
419407
}
408+
func convertTouchPointToFocusPoint(_ touchPoint: CGPoint) -> CGPoint { .init(
409+
x: touchPoint.y / cameraView.frame.height,
410+
y: 1 - touchPoint.x / cameraView.frame.width
411+
)}
420412
func configureCameraFocus(_ focusPoint: CGPoint, _ device: AVCaptureDevice) throws { try withLockingDeviceForConfiguration(device) { device in
421413
setFocusPointOfInterest(focusPoint, device)
422414
setExposurePointOfInterest(focusPoint, device)
@@ -684,7 +676,22 @@ private extension CameraManager {
684676

685677
extension CameraManager: AVCapturePhotoCaptureDelegate {
686678
public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: (any Swift.Error)?) {
687-
attributes.capturedMedia = .create(imageData: photo, orientation: frameOrientation, filters: attributes.cameraFilters)
679+
attributes.capturedMedia = .create(imageData: photo, orientation: fixedFrameOrientation(), filters: attributes.cameraFilters)
680+
}
681+
}
682+
private extension CameraManager {
683+
func fixedFrameOrientation() -> CGImagePropertyOrientation { guard UIDevice.current.orientation != attributes.deviceOrientation.toDeviceOrientation() else { return frameOrientation }
684+
return switch (attributes.deviceOrientation, attributes.cameraPosition) {
685+
case (.portrait, .front): .left
686+
case (.portrait, .back): .right
687+
case (.landscapeLeft, .back): .down
688+
case (.landscapeRight, .back): .up
689+
case (.landscapeLeft, .front) where attributes.mirrorOutput: .up
690+
case (.landscapeLeft, .front): .upMirrored
691+
case (.landscapeRight, .front) where attributes.mirrorOutput: .down
692+
case (.landscapeRight, .front): .downMirrored
693+
default: .right
694+
}
688695
}
689696
}
690697

@@ -751,6 +758,7 @@ private extension CameraManager {
751758
updateDeviceOrientation(newDeviceOrientation)
752759
updateUserBlockedScreenRotation()
753760
updateFrameOrientation()
761+
redrawGrid()
754762
}}
755763
}
756764
private extension CameraManager {
@@ -772,6 +780,9 @@ private extension CameraManager {
772780
let newFrameOrientation = getNewFrameOrientation(orientationLocked ? .portrait : UIDevice.current.orientation)
773781
updateFrameOrientation(newFrameOrientation)
774782
}}
783+
func redrawGrid() { if !orientationLocked {
784+
cameraGridView?.draw(.zero)
785+
}}
775786
}
776787
private extension CameraManager {
777788
func getNewUserBlockedScreenRotation() -> Bool { switch attributes.deviceOrientation.rawValue == UIDevice.current.orientation.rawValue {

Sources/Internal/Views/Main/CameraInputBridgeView.swift

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,84 +11,68 @@
1111

1212
import SwiftUI
1313

14-
struct CameraInputBridgeView: UIViewRepresentable {
14+
struct CameraInputBridgeView {
1515
let cameraManager: CameraManager
16-
private var inputView: UICameraInputView = .init()
17-
18-
init(_ cameraManager: CameraManager) { self.cameraManager = cameraManager }
19-
}
20-
extension CameraInputBridgeView {
21-
func makeUIView(context: Context) -> some UIView {
22-
inputView.cameraManager = cameraManager
23-
return inputView.view
24-
}
25-
func updateUIView(_ uiView: UIViewType, context: Context) {}
26-
}
27-
extension CameraInputBridgeView: Equatable {
28-
static func == (lhs: Self, rhs: Self) -> Bool { true }
16+
let inputView: UIView = .init()
2917
}
3018

3119

32-
// MARK: - UIViewController
33-
fileprivate class UICameraInputView: UIViewController {
34-
var cameraManager: CameraManager!
20+
// MARK: - PROTOCOLS CONFORMANCE
3521

3622

37-
override func viewDidLoad() {
38-
super.viewDidLoad()
3923

24+
// MARK: UIViewRepresentable
25+
extension CameraInputBridgeView: UIViewRepresentable {
26+
func makeUIView(context: Context) -> some UIView {
4027
setupCameraManager()
41-
setupTapGesture()
42-
setupPinchGesture()
43-
}
44-
override func viewDidLayoutSubviews() {
45-
super.viewDidLayoutSubviews()
46-
47-
cameraManager.fixCameraRotation()
28+
setupTapGesture(context)
29+
setupPinchGesture(context)
30+
return inputView
4831
}
32+
func updateUIView(_ uiView: UIViewType, context: Context) {}
33+
func makeCoordinator() -> Coordinator { .init(self) }
4934
}
50-
51-
// MARK: - Setup
52-
private extension UICameraInputView {
35+
private extension CameraInputBridgeView {
5336
func setupCameraManager() {
54-
cameraManager.setup(in: view)
37+
cameraManager.setup(in: inputView)
5538
}
56-
func setupTapGesture() {
57-
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
58-
view.addGestureRecognizer(tapRecognizer)
39+
func setupTapGesture(_ context: Context) {
40+
let tapRecognizer = UITapGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.onTapGesture))
41+
inputView.addGestureRecognizer(tapRecognizer)
5942
}
60-
func setupPinchGesture() {
61-
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture))
62-
view.addGestureRecognizer(pinchRecognizer)
43+
func setupPinchGesture(_ context: Context) {
44+
let pinchRecognizer = UIPinchGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.onPinchGesture))
45+
inputView.addGestureRecognizer(pinchRecognizer)
6346
}
6447
}
6548

66-
// MARK: - Gestures
67-
68-
// MARK: Tap
69-
private extension UICameraInputView {
70-
@objc func handleTapGesture(_ tap: UITapGestureRecognizer) {
71-
let touchPoint = tap.location(in: view)
72-
setCameraFocus(touchPoint)
73-
}
49+
// MARK: Equatable
50+
extension CameraInputBridgeView: Equatable {
51+
static func ==(lhs: Self, rhs: Self) -> Bool { true }
7452
}
75-
private extension UICameraInputView {
76-
func setCameraFocus(_ touchPoint: CGPoint) {
77-
do { try cameraManager.setCameraFocus(touchPoint) }
78-
catch {}
53+
54+
55+
// MARK: - LOGIC
56+
extension CameraInputBridgeView { class Coordinator: NSObject { init(_ parent: CameraInputBridgeView) { self.parent = parent }
57+
let parent: CameraInputBridgeView
58+
}}
59+
60+
// MARK: On Tap
61+
extension CameraInputBridgeView.Coordinator {
62+
@objc func onTapGesture(_ tap: UITapGestureRecognizer) {
63+
do {
64+
let touchPoint = tap.location(in: parent.inputView)
65+
try parent.cameraManager.setCameraFocus(touchPoint)
66+
} catch {}
7967
}
8068
}
8169

82-
// MARK: Pinch
83-
private extension UICameraInputView {
84-
@objc func handlePinchGesture(_ pinch: UIPinchGestureRecognizer) { if pinch.state == .changed {
85-
let desiredZoomFactor = cameraManager.attributes.zoomFactor + atan2(pinch.velocity, 33)
86-
changeZoomFactor(desiredZoomFactor)
70+
// MARK: On Pinch
71+
extension CameraInputBridgeView.Coordinator {
72+
@objc func onPinchGesture(_ pinch: UIPinchGestureRecognizer) { if pinch.state == .changed {
73+
do {
74+
let desiredZoomFactor = parent.cameraManager.attributes.zoomFactor + atan2(pinch.velocity, 33)
75+
try parent.cameraManager.changeZoomFactor(desiredZoomFactor)
76+
} catch {}
8777
}}
8878
}
89-
private extension UICameraInputView {
90-
func changeZoomFactor(_ desiredZoomFactor: CGFloat) {
91-
do { try cameraManager.changeZoomFactor(desiredZoomFactor) }
92-
catch {}
93-
}
94-
}

Sources/Public/View Protocols/Public+MCameraView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public protocol MCameraView: View {
2121

2222
// MARK: - Use-only View Methods
2323
public extension MCameraView {
24-
func createCameraView() -> some View { CameraInputBridgeView(cameraManager).equatable() }
24+
func createCameraView() -> some View { CameraInputBridgeView(cameraManager: cameraManager).equatable() }
2525
}
2626

2727
// MARK: - Use-only Logic Methods

0 commit comments

Comments
 (0)