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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39598000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:17:43  来源:igfitidea点击:

swift ios when do I need to use mediaTypes kUTTypeImage

iosswiftxcodeimageuiimagepickercontroller

提问by user2636197

I have a 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]

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

Why do you need to set mediaTypes to kUTTypeImage?

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

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

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

I select images like this through a UIAlertController:

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

回答by ovejka

kUTTypeImageis actually default for the mediaTypesproperty. 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.

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

Here is the documentation: https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619173-mediatypes

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