Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swiftui profile picture

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

    }

}
Comment

PREVIOUS NEXT
Code Example
Swift :: Swift Loop Statements 
Swift :: swift 5 uidatepicker display inline 
Swift :: image copy swift extension 
Swift :: Blinking effect on UILabel 
Swift :: how to stack align label over a card in flutter 
Swift :: Used with Collections Swift 
Swift :: Swift Modify Tuple Element 
Swift :: swift 5 for loop with index <= 
Swift :: swift 1 to n array 
Swift :: swift 5 cancel or end block operation 
Swift :: swift collectionview ispagingenabled change page size 
Swift :: remove grey background when selecting cells from uitableview swift after selection 
Swift :: swift closure 
Swift :: cifilter image preserve orientation 
Ruby :: create image from base64 ruby 
Ruby :: ruby filename from path 
Ruby :: ruby key exists 
Ruby :: Your Ruby version is 2.6.8, but your Gemfile specified 2.7.5 
Ruby :: rails video_tag with <source 
Ruby :: rails migration update column default value 
Ruby :: how to format date and time in rails 
Ruby :: sort hash ruby 
Ruby :: date time string to time in rails 
Ruby :: ruby strings 
Ruby :: text_field_tag rails 
Ruby :: ruby pry syntax 
Ruby :: ruby push array 
Ruby :: DEPRECATION WARNING: Sprockets method `register_engine` is deprecated. 
Ruby :: ruby check if hash has method before calling it 
Ruby :: rails migration populate data 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =