class myViewController
{
func selectPicture()
{
let alertSheet = UIAlertController(title:nil, message:nil, preferredStyle: .Alert)
alertSheet.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:nil))
alertSheet.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: {
(action) in
if UIImagePickerController.isSourceTypeAvailable(.Camera)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.allowsEditing = false
imagePicker.modalPresentationStyle = .OverCurrentContext
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}))
alertSheet.addAction(UIAlertAction(title: "Choose Existing", style: .Default, handler: {
(action) in
if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.modalPresentationStyle = .Popover
imagePicker.popoverPresentationController?.sourceView = self.viewToShowPopoverArrow;
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}))
self.presentViewController(alertSheet, animated: true, completion: nil)
}
}
extension myViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
self.dismissViewControllerAnimated(true) { () -> Void in
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
//Handle your image
}
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>)
{
if error != nil
{
let alert = UIAlertController(title: "Save Failed",
message: "Failed to save image",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel,
handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion:nil)
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.