//Don't Forget to add permissions in info.pList for camera and gallery struct ImagePicker{ static func callImagePicker(_ viewController: UIViewController){ let imagePicker = UIImagePickerController() imagePicker.delegate = viewController as? any UIImagePickerControllerDelegate & UINavigationControllerDelegate imagePicker.allowsEditing = true let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alertController.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action) in imagePicker.sourceType = .photoLibrary viewController.present(imagePicker, animated: true, completion: nil) })) if UIImagePickerController.isSourceTypeAvailable(.camera) { alertController.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action) in imagePicker.sourceType = .camera viewController.present(imagePicker, animated: true, completion: nil) })) } alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) viewController.present(alertController, animated: true, completion: nil) } } //Inside view Controller extension Your_View_Controller: UIImagePickerControllerDelegate, UINavigationControllerDelegate{ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage{ Your_Image_View.image = image picker.dismiss(animated: true, completion: nil) } } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.dismiss(animated: true, completion: nil) } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter