I am trying hard to understand how this works, but it's pretty hard for me. =) I have 1 view, there is one button and one small ImageView area for preview. The button triggers imagepickercontroller, and the UIView will display picked image. There is no error but the image doesn't show in the UIImageView area.
var imagePicker = UIImagePickerController()
@IBOutlet var imagePreview : UIImageView
@IBAction func AddImageButton(sender : AnyObject) {
imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
imagePicker.delegate = self
self.presentModalViewController(imagePicker, animated: true)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
imagePreview.image = tempImage
self.dismissModalViewControllerAnimated(true)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
self.dismissModalViewControllerAnimated(true)
}
Try this:
or you can also use
?
in place of!
.You're grabbing a
UIImage
namedUIImagePickerControllerOriginalImage
and there exists no such image. You're meant to grab theUIImage
with the keyUIImagePickerControllerOriginalImage
from theeditingInfo
dictionary:Since swift 4.2 and xcode 10 the
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
change a bit.at the function header the infoKey-Section changes from
didFinishPickingMediaWithInfo info: [String : Any]
todidFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]
To get the originalImage you now have to call:
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
orlet image = info[.originalImage] as? UIImage
find other infoKeys here (editedImage, imageURL and more...)
Details
Xcode 8.3, Swift 3.1
Code
Usage
ADD:
Results