ios - 确定图像选择器媒体类型是视频还是照片

时间:2016-02-13 17:27:45

标签: ios swift image video uiimagepickercontroller

我有UIImagePickerController从图库中选择照片或视频,选择项目后,我想确定选择哪一个,照片或视频

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let mediaType = info[UIImagePickerControllerMediaType]

 }

我发现了这个:How to select any Video or Movie file from UIImagePickerController

但无法转换为Swift:

 if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {

 }

提前谢谢

4 个答案:

答案 0 :(得分:11)

使用此

Name

答案 1 :(得分:4)

Swift 4.使用此:

guard info[UIImagePickerControllerMediaType] != nil else { return }
let mediaType = info[UIImagePickerControllerMediaType] as! CFString

switch mediaType {
case kUTTypeImage:

    break
case kUTTypeMovie:

    break
case kUTTypeLivePhoto:

    break
default:
    break
}

答案 2 :(得分:1)

if mediaType.isEqualToString(kUTTypeImage as String){

}

工作:)

答案 3 :(得分:0)

> Swift 4 像这样覆盖didFinishPickingMediaWithInfo

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String else {return}

    switch mediaType {
    case kUTTypeMovie:
        //handle movie here
    case kUTTypeImage:
        //handle image here
    }}