Error: 'Unsupported predicate in fetch options: mediaType == 2'

后端 未结 2 834
慢半拍i
慢半拍i 2021-02-05 13:06

I\'m trying to use smartAlbum to generate an array of either only videos or only photos or both.
You can see my code below:

    PHFetchResult *collection         


        
2条回答
  •  遥遥无期
    2021-02-05 13:31

    No need to put mediaType in predicate In swift 4.0 this is how i used fetchAsset() method from Photos framework , to get all videos from photo library.

    You can also get the video from specific folder using predicate.

       func fetchAllVideos()
    {
        //let albumName = "blah"
        let fetchOptions = PHFetchOptions()
        //        fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
        //uncomment this if you want video from custom folder
        fetchOptions.predicate = NSPredicate(format: "mediaType = %d ", PHAssetMediaType.video.rawValue )
    
        let allVideo = PHAsset.fetchAssets(with: .video, options: fetchOptions)
        allVideo.enumerateObjects { (asset, index, bool) in
            // videoAssets.append(asset)
            let imageManager = PHCachingImageManager()
            imageManager.requestAVAsset(forVideo: asset, options: nil, resultHandler: { (asset, audioMix, info) in
                if asset != nil {
                    let avasset = asset as! AVURLAsset
                    let urlVideo = avasset.url
                    print(urlVideo)
    
                }
            })
    
        }
    
    }
    

    Hope this help!!!

提交回复
热议问题