Skip to content

Commit

Permalink
fix: ImagePicker Camera로도 사용할 수 있도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
97chos committed Mar 8, 2023
1 parent 302cd8b commit 7552209
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct TakePictureView: View {
VStack {
HStack {
Spacer()

SelectPhotoButton(onSelectedImageAction: { image in
print(image)
}, content: {
Expand Down
12 changes: 8 additions & 4 deletions Sources/CameraScan/ImagePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import UIKit
// MARK: - ImagePicker

public struct ImagePicker {

private let allowsEditing: Bool
private let sourceType: UIImagePickerController.SourceType
private let onSelectedImageAction: (UIImage) -> Void
private let onDismissalAction: () -> Void

public init(
allowsEditing: Bool = false,
sourceType: UIImagePickerController.SourceType = .photoLibrary,
onSelectedImageAction: @escaping (UIImage) -> Void,
onDismissalAction: @escaping () -> Void = {})
{
self.allowsEditing = allowsEditing
self.sourceType = sourceType
self.onSelectedImageAction = onSelectedImageAction
self.onDismissalAction = onDismissalAction
}

}


Expand All @@ -26,8 +30,8 @@ extension ImagePicker: UIViewControllerRepresentable {

public func makeUIViewController(context: Context) -> some UIViewController {
let controller = UIImagePickerController()
controller.allowsEditing = false
controller.sourceType = .photoLibrary
controller.allowsEditing = allowsEditing
controller.sourceType = sourceType
controller.delegate = context.coordinator

return controller
Expand Down
9 changes: 9 additions & 0 deletions Sources/CameraScan/SwiftUI/SelectPhotoButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ public struct SelectPhotoButton<Content: View> {
private let onSelectedImageAction: (UIImage) -> Void
@State private var isShowImagePicker: Bool = false

private let allowsEditing: Bool
private let sourceType: UIImagePickerController.SourceType

public init(
allowsEditing: Bool = false,
sourceType: UIImagePickerController.SourceType = .photoLibrary,
onSelectedImageAction: @escaping (UIImage) -> Void,
@ViewBuilder content: () -> Content)
{
self.allowsEditing = allowsEditing
self.sourceType = sourceType
self.onSelectedImageAction = onSelectedImageAction
self.content = content()
}
Expand All @@ -32,6 +39,8 @@ extension SelectPhotoButton: View {
isPresented: $isShowImagePicker,
content: {
ImagePicker(
allowsEditing: allowsEditing,
sourceType: sourceType,
onSelectedImageAction: onSelectedImageAction,
onDismissalAction: { isShowImagePicker = false })
})
Expand Down

0 comments on commit 7552209

Please sign in to comment.