iosswiftuiimagepickercontroller

When do I need to use mediaTypes kUTTypeImage with UIImagePickerController?


I have an image picker in my app where you can select images from camera roll or take a new picture then upload the images to my backend server.

But looking around others' code I see that some people use this:

imagePickerController.mediaTypes = [kUTTypeImage as String]

Why do you need to set mediaTypes to kUTTypeImage?

I have not used that in my code below but everything still works fine.

I select images like this through a UIAlertController:

//Check if camera exist
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
    let cameraAction = UIAlertAction(title: "Take a photo", style: .Default) { (action) in
        self.imagePicker.sourceType = .Camera
        self.imagePicked = 1
        self.presentViewController(self.imagePicker, animated: true, completion: nil)
    }
    
    alertController.addAction(cameraAction)
}

//Check if photo lib exist
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
    let photosLibraryAction = UIAlertAction(title: "Pick image", style: .Default) { (action) in
        self.imagePicker.sourceType = .PhotoLibrary
        self.imagePicked = 1
        self.presentViewController(self.imagePicker, animated: true, completion: nil)
    }
    alertController.addAction(photosLibraryAction)
}

Then I get the images:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        //Change aspect when not dummy image
        self.bigImage.contentMode = .ScaleAspectFill
        self.bigImage.clipsToBounds = true
        self.bigImage.image = pickedImage

Solution

  • kUTTypeImage is actually default for the mediaTypes property. It states, that one can only pick still images. If you are ok with this default, you don't need to set it explicitly in your code.

    Here is the documentation:

    By default, the value of this property is the image (Swift) or kUTTypeImage (Objective-C) identifier, which designates the still camera interface when capturing media, and specifies that only still images should be displayed in the media picker when browsing saved media

    https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619173-mediatypes