You can do like here
var avatarImageView = UIImageView()
var imagePicker = UIImagePickerController()
func takePhotoFromGallery() {
imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = true
present(imagePicker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[.originalImage] as? UIImage {
avatarImageView.contentMode = .scaleAspectFill
avatarImageView.image = pickedImage
}
self.dismiss(animated: true)
}
Hope this was helpful