-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c6176e
commit 5cabe80
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Sources/SwiftUIEKtensions/Views/Representables/ImagePickerView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} | ||
|
||
} |
File renamed without changes.