2015-10-18 45 views
0

我只是試圖訪問位於名爲「MyFolder」的自定義文件夾中的照片到我的UICollectionView。我可以很容易地得到這個與下面的代碼「所有照片和視頻」的工作,但我無法弄清楚如何篩選結果只包括我的文件夾名稱:使用此PHFetchResult僅限自定義相冊

PHFetchOptions *allPhotosOptions = [PHFetchOptions new]; 
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

PHFetchResult *videos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions]; 

[videos enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { 
     //add assets to array 
}]; 

我曾嘗試:

allPhotosOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"Coached"]; 

但它不起作用。有人可以幫助我嗎?

回答

2

哇。經過約5個多小時的苦難。這很簡單。以下是可用的代碼:

__block PHAssetCollection *collection; 

// Find the album 
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"YOUR_CUSTOM_ALBUM_NAME"]; 
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum 
                 subtype:PHAssetCollectionSubtypeAny 
                 options:fetchOptions].firstObject; 

PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil]; 

[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { 


     //add assets to an array for later use in the uicollectionviewcell 

    }];