Search code examples
iosswiftuiimagepickercontrollerswiftui

How to open the ImagePicker in SwiftUI?


I need to open an image picker in my app using SwiftUI, how can I do that?

I thought about using the UIImagePickerController, but I don't know how to do that in SwiftUI.


Solution

  • Cleaned up version for Xcode 12 available via SPM as Swift Package:

    https://github.com/ralfebert/ImagePickerView

    Source:

    import SwiftUI
    
    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()
            }
    
        }
    
    }