Skip to content

Instantly share code, notes, and snippets.

@sTinGe
Last active February 12, 2018 02:14
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 sTinGe/f6b8796449eba5340396d3a9316e471c to your computer and use it in GitHub Desktop.
Save sTinGe/f6b8796449eba5340396d3a9316e471c to your computer and use it in GitHub Desktop.
default imagePicker for iOS camera
// Created by SuStinge on 12/02/2018.
import UIKit
import Foundation
import AVFoundation
import MobileCoreServices
class CameraHelper: NSObject {
public static let imagePicker = UIImagePickerController()
fileprivate let isCameraAvailable = UIImagePickerController.isSourceTypeAvailable(.camera)
fileprivate let isRearCameraAvailable = UIImagePickerController.isCameraDeviceAvailable(.rear)
fileprivate let isFrontCameraAvailable = UIImagePickerController.isCameraDeviceAvailable(.front)
fileprivate let sourceTypeCamera = UIImagePickerControllerSourceType.camera
fileprivate let rearCamera = UIImagePickerControllerCameraDevice.rear
fileprivate let frontCamera = UIImagePickerControllerCameraDevice.front
var delegate: UINavigationControllerDelegate & UIImagePickerControllerDelegate
var takePhotoIndex: Int?
init(delegate: UINavigationControllerDelegate & UIImagePickerControllerDelegate) {
self.delegate = delegate
}
func prepareImagePicker() {
if !isCameraAvailable {
return
}
let type = kUTTypeImage as String
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {
if availableTypes.contains(type) {
CameraHelper.imagePicker.mediaTypes = [type]
CameraHelper.imagePicker.sourceType = sourceTypeCamera
}
}
if isRearCameraAvailable {
CameraHelper.imagePicker.cameraDevice = rearCamera
} else if isFrontCameraAvailable {
CameraHelper.imagePicker.cameraDevice = frontCamera
}
CameraHelper.imagePicker.allowsEditing = true
CameraHelper.imagePicker.showsCameraControls = true
CameraHelper.imagePicker.delegate = delegate
print("camera ready to present")
}
func getCameraOn(_ onVC: UIViewController, _ index: Int? = nil) {
if !isCameraAvailable {
return
}
takePhotoIndex = index
onVC.present(CameraHelper.imagePicker, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment