Using native image picker system is easy and solves everyday basic needs of most apps.
UIImagePickerController
A view controller that manages the system interfaces for taking pictures, recording movies, and choosing items from the user’s media library.
Here is an example code that lets user pick images from photo library:
pickerController = UIImagePickerController()
pickerController?.delegate = self
pickerController?.mediaTypes = [“public.image”]
pickerController?.sourceType = .photoLibrary
self.present(pickerController!, animated: true, completion: nil)
We need to implement delegate methods that provides us media info from which we can extract image in our case if available. Here is a code
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage{
}
}
info
A dictionary containing the original image and the edited image, if an image was picked; or a filesystem URL for the movie, if a movie was picked. The dictionary also contains any relevant editing information. The keys for this dictionary are listed in Editing Information Keys.
Our picked image resides inside the key .originalImage which is optional so if something went wrong then it will be nil.
Now, UIImagePickerController also supports basic crop. To activate crop we set the allowsEditing property to true. By default it is false.