Last active
December 5, 2019 11:04
-
-
Save Achyut-Sagar/a968b3d00b25667919e2cfb318f223ac to your computer and use it in GitHub Desktop.
Image Picker Controller
This file contains hidden or 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
import UIKit | |
protocol ImagePickerDelegate { | |
func imagePicked(image: UIImage) | |
} | |
class ImagePicker: NSObject { | |
let presentingVC: UIViewController | |
var delegate: ImagePickerDelegate? | |
var resizeImageSize: CGFloat? | |
init(presentingVC: UIViewController,delegate: ImagePickerDelegate) { | |
self.presentingVC = presentingVC | |
self.delegate = delegate | |
} | |
func present(allowEditing: Bool, resizeImageSize: CGFloat?){ | |
self.openActionSheet(allowEditing: allowEditing) | |
self.resizeImageSize = resizeImageSize | |
} | |
} | |
//MARK: | |
extension ImagePicker{ | |
func openActionSheet(allowEditing : Bool){ | |
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) | |
//Create and add the Cancel action | |
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in | |
//Just dismiss the action sheet | |
} | |
actionSheetController.addAction(cancelAction) | |
//Create and add first option action | |
if UIImagePickerController.isSourceTypeAvailable(.camera){ | |
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take photo", style: .default) | |
{ action -> Void in | |
let imagePicker = UIImagePickerController() | |
imagePicker.delegate = self | |
imagePicker.allowsEditing = allowEditing | |
imagePicker.sourceType = .camera | |
self.presentingVC.presentFullScreen(vc: imagePicker) | |
} | |
actionSheetController.addAction(takePictureAction) | |
} | |
//Create and add a second option action | |
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Photo library", style: .default) | |
{ action -> Void in | |
let imagePicker = UIImagePickerController() | |
imagePicker.delegate = self | |
imagePicker.allowsEditing = allowEditing | |
imagePicker.sourceType = .photoLibrary | |
self.presentingVC.presentFullScreen(vc: imagePicker) | |
} | |
actionSheetController.addAction(choosePictureAction) | |
//We need to provide a popover sourceView when using it on iPad | |
actionSheetController.popoverPresentationController?.sourceView = self.presentingVC.view | |
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) | |
{ | |
actionSheetController.popoverPresentationController?.sourceRect = CGRect(x: self.presentingVC.view.bounds.midX, y: self.presentingVC.view.bounds.midY, width: 0, height: 0) | |
actionSheetController.popoverPresentationController?.permittedArrowDirections = [] | |
} | |
else | |
{ | |
// Iphone | |
} | |
self.presentingVC.present(actionSheetController, animated: true, completion: nil) | |
} | |
} | |
extension ImagePicker: UIImagePickerControllerDelegate,UINavigationControllerDelegate{ | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
let info = (picker.allowsEditing) ? | |
info[UIImagePickerController.InfoKey.editedImage] : | |
info[UIImagePickerController.InfoKey.originalImage] | |
if picker.allowsEditing{ | |
if let pickedImg = info as? UIImage { | |
var resizedImage = UIImage() | |
if resizeImageSize != nil{ | |
if pickedImg.size.width * pickedImg.scale > CGFloat(500){ | |
resizedImage = pickedImg.resizeImage(newWidth: CGFloat(500))! | |
} | |
else{ | |
resizedImage = pickedImg | |
} | |
} | |
else{ | |
resizedImage = pickedImg | |
} | |
let delay = 0.2 | |
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { | |
// Code you want to be delayed | |
self.delegate?.imagePicked(image: resizedImage) | |
} | |
} | |
} | |
self.presentingVC.dismiss(animated: true, completion: nil) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){ | |
self.presentingVC.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment