iOS开发之PHAssert的简单使用

一、获取相册中所有图片,并按时间排序

PHFetchOptions *allOptions = [[PHFetchOptions alloc]init];

allOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allOptions];

二、获取所有相册

1.系统相册

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


2.个人相册

PHFetchResult *customAlbum = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

3.打印每个相册的名称及照片数量,以个人相册举例

PHFetchResult *customAlbum = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

for (PHAssetCollection *collection in smartAlbums) {

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

NSLog(@"%@(%ld)", collection.localizedTitle, result.count);

}

三、图片的展示

获取相册的第一张图片

PHAsset *singleAsset = result.lastObject;

把PHAsset转化成我们需要的图片

[[PHImageManager defaultManager] requestImageForAsset:singleAsset targetSize:CGSizeZero contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

imageView.image = result;

}];

你可能感兴趣的:(iOS开发之PHAssert的简单使用)