Skip to content

Instantly share code, notes, and snippets.

@grosch
Last active August 29, 2015 14:24
Show Gist options
  • Save grosch/054e567c548a335ca936 to your computer and use it in GitHub Desktop.
Save grosch/054e567c548a335ca936 to your computer and use it in GitHub Desktop.
Helper class to ask for the user to pick/take a photo.
//
// GSPhotoPicker.swift
// TeamKnect
//
// Created by Scott Grosch on 10/18/14.
// Copyright (c) 2014 Gargoyle Software, LLC. All rights reserved.
//
import UIKit
final class GSPhotoPicker: NSObject {
private let onImagePicked: (UIImage?) -> Void
private weak var presentingViewController: UIViewController?
private weak var presentingView: UIView?
init(view: UIView, viewController: UIViewController, onImagePicked: (UIImage?) -> Void) {
self.onImagePicked = onImagePicked
presentingViewController = viewController
presentingView = view
}
func showImagePicker() {
let camera = UIImagePickerController.isSourceTypeAvailable(.Camera)
let library = UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)
if library && camera {
let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
sheet.addAction(UIAlertAction(title: NSLocalizedString("TAKE_PHOTO", comment: "Action button to take photo"), style: .Default) {
[unowned self] _ in
self.presentImagePicker(.Camera)
})
sheet.addAction(UIAlertAction(title: NSLocalizedString("CHOOSE_PHOTO", comment: "Action button to choose existing photo"), style: .Default) {
[unowned self] _ in
self.presentImagePicker(.PhotoLibrary)
})
sheet.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "The cancel button"), style: .Cancel, handler: nil))
presentingViewController?.presentViewController(sheet, animated: true, completion: nil)
} else if camera {
presentImagePicker(.Camera)
} else if library {
presentImagePicker(.PhotoLibrary)
}
}
private func presentImagePicker(sourceType: UIImagePickerControllerSourceType) {
if let presentingView = presentingView, presentingViewController = presentingViewController {
let camera = UIImagePickerController()
camera.sourceType = sourceType
camera.delegate = self
camera.allowsEditing = true
if sourceType == .PhotoLibrary {
// Documentation says picking from photo library on iPad must use popover.
if let popover = camera.popoverPresentationController {
camera.modalPresentationStyle = .Popover
popover.permittedArrowDirections = .Any
popover.sourceView = presentingView
popover.sourceRect = presentingView.frame
}
} else if sourceType == .Camera && UIImagePickerController.isCameraDeviceAvailable(.Front) {
camera.cameraDevice = .Front
}
presentingViewController.presentViewController(camera, animated: true, completion: nil)
}
}
}
// MARK: - UIImagePickerControllerDelegate
extension GSPhotoPicker: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
let image = (info[UIImagePickerControllerEditedImage] ?? info[UIImagePickerControllerOriginalImage]) as! UIImage
onImagePicked(image)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
onImagePicked(nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment