Skip to content

Commit

Permalink
[ADD] ImagePickerView added.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesKaraosman committed Dec 10, 2020
1 parent 3c6176e commit 5cabe80
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

}

}

0 comments on commit 5cabe80

Please sign in to comment.