diff --git a/README.md b/README.md index 52e31fd..0161dbd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Table of Contents * [Extensions](#extensions) * [Views](#views) + - [Image Picker](#image-picker) - [Mail Composer](#mail) - [Stateless Single Selection List](#stateless-single-selection-list) - [Stateful Single Selection List](#stateful-single-selection-list) @@ -16,6 +17,36 @@ Table of Contents # Views +## Image Picker + +Do not forget to add required privacy permissions. + +```swift +struct ImagePickerExampleView: View { + + @State var showImagePicker: Bool = false + @State var image: UIImage? + + var body: some View { + VStack { + if let image = image { + Image(uiImage: image) + .resizable() + .aspectRatio(contentMode: .fit) + } + Button("Pick image") { + self.showImagePicker.toggle() + } + } + .sheet(isPresented: $showImagePicker) { + ImagePickerView(sourceType: .photoLibrary) { image in + self.image = image + } + } + } +} +``` + ## Mail ```swift diff --git a/Sources/SwiftUIEKtensions/Views/Representables/ImagePickerView.swift b/Sources/SwiftUIEKtensions/Views/Representables/ImagePickerView.swift new file mode 100644 index 0000000..2b06227 --- /dev/null +++ b/Sources/SwiftUIEKtensions/Views/Representables/ImagePickerView.swift @@ -0,0 +1,88 @@ +// +// SwiftUIView.swift +// +// +// Created by Enes Karaosman on 10.12.2020. +// + +import SwiftUI + +/** + Usage; + + struct ImagePickerExampleView: View { + + @State var showImagePicker: Bool = false + @State var image: UIImage? + + var body: some View { + VStack { + if let image = image { + Image(uiImage: image) + .resizable() + .aspectRatio(contentMode: .fit) + } + Button("Pick image") { + self.showImagePicker.toggle() + } + } + .sheet(isPresented: $showImagePicker) { + ImagePickerView(sourceType: .photoLibrary) { image in + self.image = image + } + } + } + } + */ + +public struct ImagePickerView: UIViewControllerRepresentable { + + private let sourceType: UIImagePickerController.SourceType + private let onImagePicked: (UIImage) -> Void + @Environment(\.presentationMode) private var presentationMode + + public init(sourceType: UIImagePickerController.SourceType, onImagePicked: @escaping (UIImage) -> Void) { + self.sourceType = sourceType + self.onImagePicked = onImagePicked + } + + public func makeUIViewController(context: Context) -> UIImagePickerController { + let picker = UIImagePickerController() + picker.sourceType = self.sourceType + picker.delegate = context.coordinator + return picker + } + + public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {} + + public func makeCoordinator() -> Coordinator { + Coordinator( + onDismiss: { self.presentationMode.wrappedValue.dismiss() }, + onImagePicked: self.onImagePicked + ) + } + + final public class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { + + private let onDismiss: () -> Void + private let onImagePicked: (UIImage) -> Void + + init(onDismiss: @escaping () -> Void, onImagePicked: @escaping (UIImage) -> Void) { + self.onDismiss = onDismiss + self.onImagePicked = onImagePicked + } + + public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { + if let image = info[.originalImage] as? UIImage { + self.onImagePicked(image) + } + self.onDismiss() + } + + public func imagePickerControllerDidCancel(_: UIImagePickerController) { + self.onDismiss() + } + + } + +} diff --git a/Sources/SwiftUIEKtensions/Views/MailView.swift b/Sources/SwiftUIEKtensions/Views/Representables/MailView.swift similarity index 100% rename from Sources/SwiftUIEKtensions/Views/MailView.swift rename to Sources/SwiftUIEKtensions/Views/Representables/MailView.swift