I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user to choose the image from the photo gallery.
I\'ve found
click on button and open Image gallery and set image in imageview swift 3.0
add three delegate UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate
var picker:UIImagePickerController?=UIImagePickerController()
@IBOutlet var imgPhoto: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
picker?.delegate=self
}
@IBAction func btnAddPhotoClicked(_ sender: UIButton) {
openGallary()
}
func openGallary()
{
picker!.allowsEditing = false
picker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
present(picker!, animated: true, completion: nil)
}
//MARK:- ImagePicker Controller Delegate
//MARK:-
func imagePickerControllerDidCancel(_ picker:
UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imgPhoto.contentMode = .scaleToFill
imgPhoto.image = chosenImage
} else{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}