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

后端 未结 2 838
慢半拍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:18

    Use this method to get photos and videos assets array separately.

    Get All Videos

    func getAllVideos(completion:@escaping  (_ videoAssets : [PHAsset]?) -> Void) {
        var videoAssets : [PHAsset] = []
    
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
        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)
        }
        completion(videoAssets)
    }
    

    Get All Photos

    func getAllPhotos(completion:@escaping  (_ photosAssets : [PHAsset]?) -> Void) {
        var photosAssets : [PHAsset] = []
    
        let fetchOptions = PHFetchOptions()
        let scale = UIScreen.main.scale
        let screenWidth = UIScreen.main.bounds.width * scale
        let screenHeight = UIScreen.main.bounds.height  * scale
    
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
        fetchOptions.predicate = NSPredicate(format: "mediaType = %d || (mediaSubtype & %d) != 0 && (pixelHeight != %d AND pixelWidth != %d) OR (pixelHeight != %d AND pixelWidth != %d)", PHAssetMediaType.image.rawValue, PHAssetMediaSubtype.photoLive.rawValue ,screenHeight, screenWidth, screenWidth, screenHeight)
    
        let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        allPhotos.enumerateObjects { (asset, index, bool) in
            photosAssets.append(asset)
        }
        completion(photosAssets)
    }
    

提交回复
热议问题