Created
November 27, 2019 08:30
-
-
Save naveedmcs/e9100dbcc0472cf0fefb56d3ffdf6a73 to your computer and use it in GitHub Desktop.
NvImagePicker Native reusable class for IOS Swift
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
protocol NvImagePickerDelegate: class { | |
func didSelect(image: UIImage) | |
} | |
class NvImagePicker: NSObject { | |
static let shared = NvImagePicker() | |
private var delegate: NvImagePickerDelegate? | |
private struct alertPicker { | |
static var title = "Title" | |
static var message = "Please Select an Option" | |
static var camera = "Camera" | |
static var photo = "Photo Library" | |
static var cancel = "Cancel" | |
} | |
func pickerActionSheet(allowsEditingPhotoLibrary: Bool = false, | |
allowsEditingCamera: Bool = false, | |
delegate: NvImagePickerDelegate) { | |
self.delegate = delegate | |
let alert = UIAlertController(title: alertPicker.message, message: nil, preferredStyle: .actionSheet) | |
alert.addAction(UIAlertAction(title: alertPicker.photo, style: .default , handler:{ (UIAlertAction)in | |
let vc = UIImagePickerController() | |
vc.sourceType = .photoLibrary | |
vc.allowsEditing = allowsEditingPhotoLibrary | |
vc.delegate = self | |
vc.modalPresentationStyle = .fullScreen | |
let controller = UIApplication.shared.keyWindow?.rootViewController | |
controller?.present(vc, animated: true) | |
})) | |
alert.addAction(UIAlertAction(title: alertPicker.camera , style: .default , handler:{ (UIAlertAction)in | |
let vc = UIImagePickerController() | |
vc.sourceType = .camera | |
vc.isEditing = allowsEditingCamera | |
vc.delegate = self | |
let controller = UIApplication.shared.keyWindow?.rootViewController | |
controller?.present(vc, animated: true) | |
})) | |
alert.addAction(UIAlertAction(title: alertPicker.cancel , style: .cancel , handler:{ (UIAlertAction)in | |
print("Cancel") | |
})) | |
let controller = UIApplication.shared.keyWindow?.rootViewController | |
controller?.present(alert, animated: true) | |
} | |
} | |
extension NvImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
// picker.dismiss(animated: true) | |
if let image = info[.originalImage] as? UIImage { | |
print(image.size) | |
self.delegate?.didSelect(image: image) | |
picker.dismiss(animated: true) | |
} | |
if let image = info[.editedImage] as? UIImage { | |
print(image.size) | |
self.delegate?.didSelect(image: image) | |
picker.dismiss(animated: true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example
`import UIKit
class ViewController: UIViewController {
} `
extension ViewController: NvImagePickerDelegate { func didSelect(image: UIImage) { self.imageView.image = image } }