Take Photos Using UIImagePickerController.

The UIImagePickerController class used for taking Photos and movies from Photo Library or Camera. You can choose image or Movie which is saved in PhotoLibrary or Camera. 



The UIImagePickerController manages the user interface and giving the result of the user in its delegate method. This tutorials for both Movie and Photos. You have to select sourceType from where you want to Photos or Movie.

If you select SourceType  UIImagePickerControllerSourceTypeCamera provide user for new Photos or Movie from supported device. and select  SourceType UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum provide to choose photos or movie from Saved.


Step 1 : make one object of UIImagePickerController.

var imagPickUp : UIImagePickerController!

Step 2 : Adding delegate method of UIImagePickerController in ViewController interface.

class NewAddEventViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {

Step 3 : Create one function for init UIImagePickerController which return its object. 

func imageAndVideos()-> UIImagePickerController{
    if(imagPickUp == nil){
        imagPickUp = UIImagePickerController()
        imagPickUp.delegate = self
        imagPickUp.allowsEditing = false
    }
    return imagPickUp
}

Step 4 : Calling the function imageAndVideos in ViewDidLoad method. and get the init object of UIImagePickerController.

override func viewDidLoad() {
    super.viewDidLoad()
    self.imageAndVideos()
}


Step 5 : Take one UIActionSheet from where you want to take a image. Action sheet have two option. Camera and PhotoLibrary.

let ActionSheet = UIAlertController(title: nil, message: "Select Photo", preferredStyle: .ActionSheet)

   let cameraPhoto = UIAlertAction(title: "Camera", style: .Default, handler: {
            (alert: UIAlertAction) -> Void in
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

              self.imagPickUp.mediaTypes = [kUTTypeImage as String]

              self.imagPickUp.sourceType = UIImagePickerControllerSourceType.Camera;
              self.presentViewController(self.imagPickUp, animated: true, completion: nil)
            }
            else{
                UIAlertView(title: "SociaLog", message: "No Camera available.", delegate: nil, cancelButtonTitle: "OK").show()
            }

        })

   let PhotoLibrary = UIAlertAction(title: "Photo Library", style: .Default, handler: {
            (alert: UIAlertAction) -> Void in
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
              self.imagPickUp.mediaTypes = [kUTTypeImage as String]
              self.imagPickUp.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
              self.presentViewController(self.imagPickUp, animated: true, completion: nil)
            }

        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (alert: UIAlertAction) -> Void in

        })

        ActionSheet.addAction(cameraPhoto)
        ActionSheet.addAction(PhotoLibrary)
        ActionSheet.addAction(cancelAction)


  if UIDevice.currentDevice().userInterfaceIdiom == .Pad{
            let presentC : UIPopoverPresentationController  = ActionSheet.popoverPresentationController!
            presentC.sourceView = sender as? UIView
            presentC.sourceRect = sender.bounds
           self.presentViewController(ActionSheet, animated: true, completion: nil)

        }
        else{
            self.presentViewController(ActionSheet, animated: true, completion: nil)
        }

Step 6 : UIImagePickerController Delegate method where your image result and take image from delegate use the image in your apps.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
        let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        picker.dismissViewControllerAnimated(true, completion: { () -> Void in
            // Dismiss
        })
    }
func imagePickerControllerDidCancel(picker: UIImagePickerController){
        picker.dismissViewControllerAnimated(true, completion: { () -> Void in
            // Dismiss
        })
    }

Thanks 

Take Photos Using UIImagePickerController. Take Photos Using UIImagePickerController. Reviewed by Kirit Modi on 05:47:00 Rating: 5

No comments:

Powered by Blogger.