For Xcode 8.1 with quick 3, After changing the delegate method as shown below
change your
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imagePost.image = image self.dismiss(animated: true, completion: nil) }
for
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage picker.dismiss(animated: true, completion: nil) }
I forgot to add a UINavigationControllerDelegate along with a UIImagePickerControllerDelegate in
class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
You would add a variable at the beginning, for example
var imagePicker = UIImagePickerController ()
and set the delegate and call the function in viewdidload () as
imagePicker.delegate = self viwImagePick()
Then explain that the function <
//ImagePicker func viwImagePick(){ let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("Camera selected") self.openCamera() //Code for Camera //cameraf }) alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("Photo selected") self.openGallary() //Code for Photo library //photolibaryss }) self.present(alert, animated: true, completion: nil) } func openCamera() { imagePicker.sourceType = UIImagePickerControllerSourceType.camera if UIDevice.current.userInterfaceIdiom == .phone { self.present(imagePicker, animated: true, completion: nil) } else { let popover = UIPopoverController(contentViewController: imagePicker) popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true) } } func openGallary() { imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum if UIDevice.current.userInterfaceIdiom == .phone { self.present(imagePicker, animated: true, completion: nil) } else { let popover = UIPopoverController(contentViewController: imagePicker) popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true) } }
Now the image is added to the image view from the library