Skip to content

Instantly share code, notes, and snippets.

@sTinGe
Created 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/fcfb7955ad4d437bcb2ba9fe76a80c1a to your computer and use it in GitHub Desktop.
Save sTinGe/fcfb7955ad4d437bcb2ba9fe76a80c1a to your computer and use it in GitHub Desktop.
default photo library for iOS camera roll
// Created by SuStinge on 12/02/2018.
import UIKit
import Foundation
import AVFoundation
import MobileCoreServices
class PhotoLibraryHelper: NSObject {
public static let imagePicker = UIImagePickerController()
fileprivate let isPhotoLibraryAvailable = UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
fileprivate let sourceTypePhotoAlbum = UIImagePickerControllerSourceType.photoLibrary
var delegate: UINavigationControllerDelegate & UIImagePickerControllerDelegate
var takePhotoIndex: Int?
init(delegate: UINavigationControllerDelegate & UIImagePickerControllerDelegate) {
self.delegate = delegate
}
func prepareImagePicker() {
if !isPhotoLibraryAvailable {
return
}
let type = kUTTypeImage as String
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary) {
if availableTypes.contains(type) {
PhotoLibraryHelper.imagePicker.mediaTypes = [type]
PhotoLibraryHelper.imagePicker.sourceType = sourceTypePhotoAlbum
}
}
PhotoLibraryHelper.imagePicker.allowsEditing = true
PhotoLibraryHelper.imagePicker.delegate = delegate
print("photo library ready to present")
}
func getPhotoLibraryOn(_ onVC: UIViewController, _ index: Int? = nil) {
if !isPhotoLibraryAvailable {
return
}
takePhotoIndex = index
onVC.present(PhotoLibraryHelper.imagePicker, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment