Reputation: 365
I am trying to select an image using the UIImagePickerController . I have the following code in which there is one IBOutlet of UIImageView.
After selecting the image in didFinishPickingMediaWithInfo I get the image in memory but somehow it is not visible in the imageview. I did try to change a static image in didFinishPickingMediaWithInfo for imageview but it didn't show the image either. The code is as below,
class SnapshotViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate
{
@IBOutlet var imageView: UIImageView!
@IBAction func takePhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
dispatch_async(dispatch_get_main_queue())
{
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imageView.image = image
}
}
}
I guess this is general problem that whatever image i am changing for this imageView in didFinishPickingMediaWithInfo is not working. If i change the imageView image in viewDidLoad then it works. Can anyone please help as i need to show the image selected via photo library?
Upvotes: 0
Views: 1435
Reputation: 22477
In swift 4, I tried various things but non of them work until I just add @objc
annotation to didFinishPickingMediaWithInfo
override method:
Pressenting UIImagePickerController
:
@objc func handleSelectProfileImageView () {
let pickerController = UIImagePickerController()
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}
Extension to get media data (in this one I had to add @objc
):
extension MyViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImage = image
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
About @objc
from doc:
Apply this attribute to any declaration that can be represented in Objective-C—for example, nonnested classes, protocols, nongeneric enumerations (constrained to integer raw-value types), properties and methods (including getters and setters) of classes, protocols and optional members of a protocol, initializers, and subscripts. The objc attribute tells the compiler that a declaration is available to use in Objective-C code.
Upvotes: 0
Reputation: 31
I think this code would solve your problem and make the picture from your photolibrary appear:
class PostViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet var imageToPost: UIImageView!
@IBAction func chooseAnImage(_ sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageToPost.image = image
}
self.dismiss(animated: true, completion: nil)
}
Upvotes: 0
Reputation: 880
It is strongly recommended not to update UI controls from a background thread. This can be the cause of strange behaviour and crashes which are sometimes very hard to identify.
Try to set the image on the main thread instead:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imageView.image = image
dismissViewControllerAnimated(true, completion: nil)
}
Upvotes: 0