Skip to content

Instantly share code, notes, and snippets.

@stansidel
Created July 4, 2016 11:15
Show Gist options
  • Save stansidel/beb82fe916b3a6d64e474079d4d8e43b to your computer and use it in GitHub Desktop.
Save stansidel/beb82fe916b3a6d64e474079d4d8e43b to your computer and use it in GitHub Desktop.
Presenting UIAlertController for uploading a photo
@IBAction func loadImage(sender: AnyObject) {
let imagePickerActionSheet = UIAlertController(title: "Take/Upload Photo",
message: nil, preferredStyle: .ActionSheet)
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
let cameraButton = UIAlertAction(title: "Take Photo", style: .Default, handler: { (alert) in
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
self.presentViewController(imagePicker, animated: true, completion: nil)
})
imagePickerActionSheet.addAction(cameraButton)
}
let libraryButton = UIAlertAction(title: "Choose Existing", style: .Default) { (alert) in
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
imagePickerActionSheet.addAction(libraryButton)
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel) { (alert) in }
imagePickerActionSheet.addAction(cancelButton)
presentViewController(imagePickerActionSheet, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment