仅从资产集合类型 PHAssetCollectionTypeSmartAlbum 中获取类型为 PHAssetMediaTypeImage 的照片
Fetch only photos of type PHAssetMediaTypeImage form asset collection type PHAssetCollectionTypeSmartAlbum
我正在使用照片框架来获取 iOS8 中的相册列表。我可以使用
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
这为我提供了包括视频在内的所有智能相册的列表。如何从此列表中过滤掉视频。我只需要图像。
帮助将不胜感激。谢谢
您应该设置提取选项,它有一个 属性 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.
}
}
}
要在 swift 中做同样的事情:
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)
我正在使用照片框架来获取 iOS8 中的相册列表。我可以使用
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
这为我提供了包括视频在内的所有智能相册的列表。如何从此列表中过滤掉视频。我只需要图像。
帮助将不胜感激。谢谢
您应该设置提取选项,它有一个 属性 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.
}
}
}
要在 swift 中做同样的事情:
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)