Last active
October 21, 2020 17:47
-
-
Save matteodanelli/840ed6225990df54d9d7b2926863968b to your computer and use it in GitHub Desktop.
Create iOS alert controller for image and video selection, from camera or filesystem
This file contains hidden or 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
class ImagePicker: UIViewController, UINavigationControllerDelegate { | |
@IBOutlet var pictureView: UIImageView! | |
func tapAddPicture(_ sender: Any) { | |
let imagePickerController = UIImagePickerController() | |
imagePickerController.delegate = self | |
imagePickerController.mediaTypes = ["public.image", "public.movie"] | |
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) | |
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { (action:UIAlertAction) in | |
if UIImagePickerController.isSourceTypeAvailable(.camera) { | |
imagePickerController.sourceType = .camera | |
self.present(imagePickerController, animated: true, completion: nil) | |
} else { | |
self.showAlert(withTitle: "Missing camera", andMessage: "You can't take photo, there is no camera.") | |
} | |
})) | |
actionSheet.addAction(UIAlertAction(title: "Choose From Library", style: .default, handler: { (action:UIAlertAction) in | |
imagePickerController.sourceType = .photoLibrary | |
self.present(imagePickerController, animated: true, completion: nil) | |
})) | |
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) | |
if pictureView.image != nil { | |
actionSheet.addAction(UIAlertAction(title: "Remove Photo", style: .destructive, handler: {(_ action: UIAlertAction) -> Void in | |
self.pictureView.image = nil | |
})) | |
} | |
present(actionSheet, animated: true, completion: nil) | |
} | |
func showAlert(withTitle title: String, andMessage message: String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in | |
}) | |
alert.addAction(defaultAction) | |
present(alert, animated: true, completion: {() -> Void in | |
alert.view.tintColor = UIColor.green | |
}) | |
} | |
} |
This file contains hidden or 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
extension ImagePicker: UIImagePickerControllerDelegate { | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
print(info) | |
let mediaType = info["UIImagePickerControllerMediaType"] as! String | |
switch mediaType { | |
case "public.movie": | |
print("Movie selected!") | |
// TODO: Load video | |
picker.dismiss(animated: true, completion: nil) | |
break | |
case "public.image": | |
print("Image selected!") | |
let image = info[UIImagePickerControllerOriginalImage] as! UIImage | |
self.pictureView.image = image | |
picker.dismiss(animated: true, completion: nil) | |
break | |
default: | |
picker.dismiss(animated: true, completion: nil) | |
return | |
} | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
picker.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment