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

时间:2016-09-20 15:23:35

标签: ios swift xcode image uiimagepickercontroller

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

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

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

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

我通过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)
        }

然后我得到了图片:

 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

1 个答案:

答案 0 :(得分:3)

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

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