To access the camera roll you want to use the Image Picker.
(Swift 2.2)
Swift 3.0
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { var imagePicker: UIImagePickerController! @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imagePicker = UIImagePickerController() imagePicker.delegate = self } @IBAction func SelectImage_TouchUpInside(sender: AnyObject) { present(imagePicker, animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePicker.dismiss(animated: true, completion: nil) imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage } }In iOS 10 you also need to give a usage description in your Info.plist
Swift 2.2
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { var imagePicker: UIImagePickerController! @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imagePicker = UIImagePickerController() imagePicker.delegate = self } @IBAction func SelectImage_TouchUpInside(sender: AnyObject) { presentViewController(imagePicker, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { imagePicker.dismissViewControllerAnimated(true, completion: nil) imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage } // Deprecated // func imagePickerController(picker: UIImagePickerController, // didFinishPickingImage image: UIImage, // editingInfo: [String : AnyObject]?) { // imagePicker.dismissViewControllerAnimated(true, completion: nil) // imageView.image = image // } }
(Swift 2.2)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.