Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing picture library in an ActionSheet style

I'm trying to create a profilePic in a Sign Up View Controller. The profilePic is of type UIImageView. I want to be able to tap it so it instantly pulls up a photo library in an ActionSheet Style. And to also have one of the image box a button to access the camera. Is this possible?

import UIKit

class SignUpViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var profilePic: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // PROFILE PICTURE

        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")
        }
    }
like image 541
Martin Q Avatar asked Jul 17 '15 14:07

Martin Q


2 Answers

Try this code below :

import UIKit

class 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 !

like image 145
iRiziya Avatar answered Nov 12 '22 12:11

iRiziya


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

like image 37
Maor Avatar answered Nov 12 '22 14:11

Maor