Skip to content

Instantly share code, notes, and snippets.

@naveedmcs
Created November 27, 2019 08:30
Show Gist options
  • Save naveedmcs/e9100dbcc0472cf0fefb56d3ffdf6a73 to your computer and use it in GitHub Desktop.
Save naveedmcs/e9100dbcc0472cf0fefb56d3ffdf6a73 to your computer and use it in GitHub Desktop.
NvImagePicker Native reusable class for IOS Swift
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)
}
}
}
@naveedmcs
Copy link
Author

naveedmcs commented Nov 27, 2019

Usage Example

`import UIKit
class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  
   
}

@IBAction func buttonTapped (sender: UIButton) {
    let picker = NvImagePicker.shared
    picker.pickerActionSheet(allowsEditingPhotoLibrary: true,delegate: self)
    
}

} `

extension ViewController: NvImagePickerDelegate { func didSelect(image: UIImage) { self.imageView.image = image } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment