Created
February 5, 2024 10:49
-
-
Save saud978/6452f7d094d4bcb10356eaa83c60afa8 to your computer and use it in GitHub Desktop.
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
// | |
// ImagePicker.swift | |
// Image for Instagram | |
// | |
// Created by Saud Almutlaq on 09/05/2020. | |
// Copyright © 2020 Saud Soft. All rights reserved. | |
// Code from https://stackoverflow.com/questions/24625687/swift-uiimagepickercontroller-how-to-use-it | |
import AVFoundation | |
import Photos | |
import UIKit | |
protocol ImagePickerDelegate: class { | |
func imagePickerDelegate(canUseCamera accessIsAllowed: Bool, delegatedForm: ImagePicker) | |
func imagePickerDelegate(canUseGallery accessIsAllowed: Bool, delegatedForm: ImagePicker) | |
func imagePickerDelegate(didSelect image: UIImage, delegatedForm: ImagePicker) | |
func imagePickerDelegate(didCancel delegatedForm: ImagePicker) | |
} | |
class ImagePicker: NSObject { | |
private weak var controller: UIImagePickerController? | |
weak var delegate: ImagePickerDelegate? = nil | |
func present(parent viewController: UIViewController, sourceType: UIImagePickerController.SourceType) { | |
let controller = UIImagePickerController() | |
controller.delegate = self | |
controller.sourceType = sourceType | |
self.controller = controller | |
DispatchQueue.main.async { | |
viewController.present(controller, animated: true, completion: nil) | |
} | |
} | |
func dismiss() { controller?.dismiss(animated: true, completion: nil) } | |
} | |
extension ImagePicker { | |
private func showAlert(targetName: String, completion: @escaping (Bool)->()) { | |
DispatchQueue.main.async { [weak self] in | |
guard let self = self else { return } | |
let alertVC = UIAlertController(title: "Access to the \(targetName)", | |
message: "Please provide access to your \(targetName)", | |
preferredStyle: .alert) | |
alertVC.addAction(UIAlertAction(title: "Settings", style: .default, handler: { action in | |
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString), | |
UIApplication.shared.canOpenURL(settingsUrl) else { completion(false); return } | |
UIApplication.shared.open(settingsUrl, options: [:]) { | |
[weak self] _ in self?.showAlert(targetName: targetName, completion: completion) | |
} | |
})) | |
alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in completion(false) })) | |
UIApplication.shared.delegate?.window??.rootViewController?.present(alertVC, animated: true, completion: nil) | |
} | |
} | |
func cameraAsscessRequest() { | |
if AVCaptureDevice.authorizationStatus(for: .video) == .authorized { | |
delegate?.imagePickerDelegate(canUseCamera: true, delegatedForm: self) | |
} else { | |
AVCaptureDevice.requestAccess(for: .video) { [weak self] granted in | |
guard let self = self else { return } | |
if granted { | |
self.delegate?.imagePickerDelegate(canUseCamera: granted, delegatedForm: self) | |
} else { | |
self.showAlert(targetName: "camera") { self.delegate?.imagePickerDelegate(canUseCamera: $0, delegatedForm: self) } | |
} | |
} | |
} | |
} | |
func photoGalleryAsscessRequest() { | |
PHPhotoLibrary.requestAuthorization { [weak self] result in | |
guard let self = self else { return } | |
if result == .authorized { | |
DispatchQueue.main.async { [weak self] in | |
guard let self = self else { return } | |
self.delegate?.imagePickerDelegate(canUseGallery: result == .authorized, delegatedForm: self) | |
} | |
} else { | |
self.showAlert(targetName: "photo gallery") { self.delegate?.imagePickerDelegate(canUseCamera: $0, delegatedForm: self) } | |
} | |
} | |
} | |
} | |
extension ImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
if let image = info[.editedImage] as? UIImage { | |
delegate?.imagePickerDelegate(didSelect: image, delegatedForm: self) | |
return | |
} | |
if let image = info[.originalImage] as? UIImage { | |
delegate?.imagePickerDelegate(didSelect: image, delegatedForm: self) | |
} else { | |
print("Other source") | |
} | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
delegate?.imagePickerDelegate(didCancel: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment