我在我的应用程序中有一个图像选择器,您可以从相机卷中选择图像或拍摄新照片,然后将图像上载到我的后端服务器。
但是环顾其他代码,我发现有些人使用这个:
imagePickerController.mediaTypes=[kUTTypeImage作为字符串]
为什么需要将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
最佳答案
kUTTypeImage
实际上是mediaTypes
属性的默认值。它说,一个人只能选择静止图像。如果可以使用此默认值,则不需要在代码中显式设置它。
以下是文档:https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619173-mediatypes