iOS 8照片框架:获取特定的相册列表

时间:2014-11-21 07:40:00

标签: ios objective-c phasset

除了我自己相册中的照片外,我试图让所有照片都得到。 我尝试使用此答案中的方法获取所有照片。 ios - Simple example to get list of photo albums? 但我找不到过滤自己专辑的选项。

我使用下面的代码...我可以获得除了我自己的专辑之外的专辑,但除了我自己专辑中的照片外,我怎样才能获得所有照片的PHFetchResult?

谢谢。

PHFetchOptions *albumsFetchOption = [[PHFetchOptions alloc]init];
NSString *string = @"MyCam";
albumsFetchOption.predicate = [NSPredicate predicateWithFormat:@"title != %@",string];

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:albumsFetchOption];

1 个答案:

答案 0 :(得分:2)

最后,我使用NSArray而不是PHFetchResult ...

//获取所有

PHFetchOptions *allPhotosfetchOption = [[PHFetchOptions alloc]init];
allPhotosfetchOption.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
allPhotosfetchOption.predicate = [NSPredicate predicateWithFormat:@"mediaType == 1"];

__block NSMutableArray *allAssetArray = [NSMutableArray new];
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosfetchOption];
[result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
        [allAssetArray addObject:asset];
}];

//MyCam Album 
NSString *string = @"MyCam";
PHFetchOptions *albumFetchOption = [[PHFetchOptions alloc]init];
albumFetchOption.predicate = [NSPredicate predicateWithFormat:@"title == %@",string];
PHFetchResult *albumResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:albumFetchOption];

[albumResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
    PHFetchResult *loftAssetResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    [loftAssetResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
                if ([allAssetArray containsObject:asset]) {
                    [allAssetArray removeObject:asset];
                }
    }];
}];