Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - Determine if image picker media type is video or photo

I have UIImagePickerController which is choosing photo or video from gallery, after selecting item, I want to determine which one is chosen, photo or video

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

 }

I've found this: How to select any Video or Movie file from UIImagePickerController

but could not translate to Swift:

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

 }

thank you in advance

like image 890
Salome Tsiramua Avatar asked Feb 13 '16 17:02

Salome Tsiramua


2 Answers

Use this

if let mediaType = info[UIImagePickerControllerMediaType] as? String {

    if mediaType  == "public.image" {
        print("Image Selected")
    }

    if mediaType == "public.movie" {
        print("Video Selected")
    }
}
like image 103
Phani Bob Avatar answered Nov 15 '22 06:11

Phani Bob


Swift 4. Use this:

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
}
like image 41
Geoff H Avatar answered Nov 15 '22 04:11

Geoff H