Accessing picture library in an ActionSheet style
Try this code below :
import UIKitclass SignUpViewController: UIViewController, UIImagePickerControllerDelegate {@IBOutlet weak var profilePic: UIImageView!override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:") profilePic.addGestureRecognizer(tapGesture) profilePic.userInteractionEnabled = true}func imageTapped(gesture:UIGestureRecognizer) { if let profile1Pic = gesture.view as? UIImageView { print("Image Tapped") showActionSheet() }}func camera(){ var myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(myPickerController, animated: true, completion: nil)}func photoLibrary(){ var myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(myPickerController, animated: true, completion: nil)}func showActionSheet() { let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in self.camera() })) actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in self.photoLibrary() })) actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)) self.presentViewController(actionSheet, animated: true, completion: nil)}func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { profilePic.image = info[UIImagePickerControllerOriginalImage] as? UIImage self.dismissViewControllerAnimated(true, completion: nil)}}
Its working on my side..hope will do the some for you !
I recently published a GitHub repository to handle this type of Action Sheet.
You can download this class from here : MSActionSheet
and simply write :
EDIT :
● Now supporting iPad
MSActionSheet(viewController: self, sourceView: sender).showFullActionSheet { sender.setImage($0, for: .normal)}
MSActionSheet : is the best library for set user's profile picture.
- I used this library but it's not supported in iPad so icustomise for some functions for support to iPad and Display withpopOverController.
Below the step of customisation.
Replace function with
showFullActionSheet
func showFullActionSheet(on vc : UIViewController, over btn : UIButton,handler : @escaping (_ : UIImage?) -> Void) { let sheet = create().addLibrary().addFrontCamera().addRearCamera().addCancelButton() sheet.show(on: vc,over: btn){ (image : UIImage?) in handler(image) }}
Add code before
vc.present(MSActionSheet.sheet!, animated: true, completion: nil)
in functionshow
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad { if let presenter = MSActionSheet.sheet?.popoverPresentationController { presenter.sourceView = btn presenter.sourceRect = btn.bounds } }
Add below code before
clientVC?.present(picker, animated: true, completion: nil)
in functionhandler
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad { picker.modalPresentationStyle = .popover if let presenter = picker.popoverPresentationController { presenter.sourceView = sourceBtn presenter.sourceRect = sourceBtn!.bounds } }
How to use
let msActionSheet = MSActionSheet.instance msActionSheet.showFullActionSheet(on: self,over: btn){ (image) in btn.setImage(image, for: .normal) } msActionSheet.tintColor(color: Constants.kColorBluish)