2

I want to check what type the latestObject is. Here's some code:

allMedia = PHAsset.fetchAssetsWithOptions(fetchOptions)
let allPhotos = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
let allVideo = PHAsset.fetchAssetsWithMediaType(.Video, options: fetchOptions)
print("Found \(allMedia.count) media")
print("Found \(allPhotos.count) images")
print("Found \(allVideo.count) videos")

let latestObject: AnyObject! = allMedia.lastObject

// How to check what type latestObject is?
// I think something with mediaType but how is it exactly going?
mafioso
  • 1,630
  • 4
  • 25
  • 45
  • check : 1) http://stackoverflow.com/questions/31582717/how-to-check-a-file-is-video-or-image 2)http://stackoverflow.com/questions/17145844/iphone-how-to-check-if-the-file-is-a-directory-audio-video-or-image – sohil Jul 27 '16 at 10:54

2 Answers2

8

Have you tried something like this:

if let asset = allMedia.lastObject as? PHAsset {
    switch asset.mediaType {
    case .Image:
        print("Image")
    case .Video:
        print("Video")
    case .Audio:
        print("Audio")
    default:
        print("Unknown")
    }
}

Each element contained in the PHFetchResult is a PHAsset (in this case). So, with casting to PHAsset, you can access the property mediaType.

OOPer
  • 47,149
  • 6
  • 107
  • 142
0
You can check it in UIImagePickerController delegate method 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

  if((info["UIImagePickerControllerMediaType"] as! String) == "public.movie" ){
    // Video file
  }
  else{
    // Image
  } 
}
Adii
  • 23
  • 5