Friday, 4 March 2016

UIImagePickerController in Swift

UIImagePickerController  class manages customizable, system-supplied user interface for taking pictures and movies, and for choosing saved images and movies for use in the app. A UIImagePickerController manages user interactions and delivers the results of those interactions to a delegate object.
For more details Click here.

To use an image picker controller with in the app, perform these steps:

1) Verify that the device is capable of picking content from the desired source.
2) Check which media types are available for the source type using.
3) Adjust image picker controller's UI according to media type
4) Present user interface.


Code:

// MARK: - Open Camera
    func openCamera(source:UIImagePickerControllerSourceType){


//Check source type available or not
        if(UIImagePickerController.isSourceTypeAvailable(source) == false){
            return;
        }

//Create new object of UIImagePickerContrller
        let imagepickerController = UIImagePickerController()
       imagepickerController.sourceType = source
        imagepickerController.delegate = self

//Present UIImagePickerContrller user interface
        self.presentViewController(imagepickerController, animated: true, completion: nil)
    }


//MARK: - Image Picker Delegate
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        nameTitlelabel.hidden = false
        //dismiss image picker controller view
        picker.dismissViewControllerAnimated(true, completion: nil)
    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

//Get selected image
        let seletctedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        patientImageView.image = seletctedImage

//dismiss image picker controller view
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

No comments:

Post a Comment