Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - UIImagePickerController - how to use it?

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) } 
like image 352
user3806731 Avatar asked Jul 08 '14 07:07

user3806731


People also ask

What is UIImagePickerController?

A view controller that manages the system interfaces for taking pictures, recording movies, and choosing items from the user's media library.

How do I open the camera in Swift 4?

To access the camera in Swift, we first must ask the user for permission. Open up your Info. plist file from your Project Navigator. Then right click and click Add Row .


1 Answers

You're grabbing a UIImage named UIImagePickerControllerOriginalImage and there exists no such image. You're meant to grab the UIImage with the key UIImagePickerControllerOriginalImage from the editingInfo dictionary:

let tempImage = editingInfo[UIImagePickerControllerOriginalImage] as! UIImage 
like image 170
Jesper Avatar answered Oct 21 '22 12:10

Jesper