Skip to content

Instantly share code, notes, and snippets.

@kennylugo
Last active March 2, 2016 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennylugo/308a15c7961b6b2e964b to your computer and use it in GitHub Desktop.
Save kennylugo/308a15c7961b6b2e964b to your computer and use it in GitHub Desktop.
Photo Library & Camera
//DON'T FORGET TO ADD TWO CLASSES: UIIMAGEPICKERCONTROLLERDELEGATE, UINAVIGATIONCONTROLLERDELEGATE
//didSelectRowAtIndexPath: allows us to select cell from a tableView
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//indexPath.row : allows us to select cells, in our case we're selecting the first cell
if indexPath.row == 0 {
//If the photo library is available
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
//instance of the controller, allows us to use it's methods
let imagePicker = UIImagePickerController()
//where to get the image from
imagePicker.sourceType = .PhotoLibrary
imagePicker.editing = false
//Present the controller
self.presentViewController(imagePicker, animated: true, completion: nil)
//who will notify the image picker that something was picked
imagePicker.delegate = self
}
}
//will deselect cells as soon as I select it, makes the tableview experience feel better
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
//this function comes from the UIImagePickerController, allowing us to do something after selecting an image
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//store the selected image inside the image view
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
//Dismiss the controller
self.dismissViewControllerAnimated(true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment