swift ios 什么时候需要使用mediaTypes kUTTypeImage

swift ios when do I need to use mediaTypes kUTTypeImage

我的应用程序中有一个图像选择器,您可以在其中 select 相机胶卷中的图像或拍摄新照片,然后将图像上传到我的后端服务器。

但是环顾其他人的代码,我发现有些人使用这个: imagePickerController.mediaTypes = [kUTTypeImage as String]

为什么需要将 mediaTypes 设置为 kUTTypeImage?

我没有在下面的代码中使用它,但一切仍然正常

我通过 UIAlertController select 这样的图像:

//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)
        }

然后我得到图像:

 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

kUTTypeImage 实际上是 mediaTypes 属性 的默认设置。它指出,只能选择静止图像。如果您接受此默认值,则无需在代码中明确设置它。

这是文档:https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619173-mediatypes