美文网首页
iOS开发之PHAssert的简单使用

iOS开发之PHAssert的简单使用

作者: 雄雄鹰 | 来源:发表于2017-12-04 17:08 被阅读0次

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

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的简单使用

      本文链接:https://www.haomeiwen.com/subject/afsxixtx.html