Last active
February 12, 2018 02:14
-
-
Save sTinGe/f6b8796449eba5340396d3a9316e471c to your computer and use it in GitHub Desktop.
default imagePicker for iOS camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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