仅获取PHAssetMediaTypeImage类型的照片,表单资产集合类型PHAssetCollectionTypeSmartAlbum

时间:2015-01-22 12:58:02

标签: ios photolibrary photokit phasset

我正在使用Photos框架来获取iOS8中的专辑列表。我可以使用

来做到这一点
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

这为我提供了所有智能相册的列表,包括视频。如何过滤此列表中的视频。我只需要图像。

帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:26)

您应该设置提取选项,它有一个属性predicate,可用于过滤视频。以下是这样做的一个例子。

PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

//set up fetch options, mediaType is image.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];

for (NSInteger i =0; i < smartAlbums.count; i++) {
    PHAssetCollection *assetCollection = smartAlbums[i];
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];

    NSLog(@"sub album title is %@, count is %ld", assetCollection.localizedTitle, assetsFetchResult.count);
    if (assetsFetchResult.count > 0) {
        for (PHAsset *asset in assetsFetchResult) {
            //you have got your image type asset.
        }
    }
}

答案 1 :(得分:22)

在swift中做同样的事情:

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)