2016-07-27 25 views
1

我想檢查latestObject是什麼類型。下面是一些代碼:檢查提取的對象是Swift中的Image還是Video

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? 
+0

檢查: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

回答

3

你有沒有嘗試過這樣的事情:

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") 
    } 
} 

包含在PHFetchResult每個元素都是一個PHAsset(在這種情況下)。 因此,在鑄造到PHAsset時,您可以訪問屬性mediaType

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 
    } 
} 
+0

我不使用UIImagePickerController – mafioso

相關問題