Thursday, May 12, 2016

Accessing the Camera Roll

To access the camera roll you want to use the Image Picker.

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.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...