//查看是否授权了。
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized){
NSLog(@"没有授权");
}
//PHAsset 可以表示一个图片的信息 相册信息
//PHFetchResult 数组
//1.读取所有相册的信息
PHFetchResult *collectonResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumScreenshots|PHAssetCollectionSubtypeSmartAlbumVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
//2. 遍历整个相册,对某一个具体相册进行操作
[collectonResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//这个相册的信息
PHAssetCollection *assetCollection = (PHAssetCollection *)obj;
//按照创建时间的先后顺序查找
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
//获取这个相册里面所有图片的信息
PHFetchResult *albumResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
//获取相册的名字
NSString *albumName = assetCollection.localizedTitle;
//这个相册的相片个数
NSInteger count = albumResult.count;
NSLog(@"%@ (%ld)", albumName, count);
//读取相册里面的最近的一张图片信息PHAsset
PHAsset *lastAsset = [albumResult lastObject];
/*
targetSize: 需要读取的图片的尺寸
contentMode: 图片以什么方式拉伸 (AspectFit)
*/
//读取图片的选项 异步
/*
resizeMode:
exact:高清图片 原图 当选择原图 那么targetSize必须是原图的大小
PHImageManagerMaximumSize
fast: 以最快的速度读取 (像素低)
none:
*/
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
__weak ViewController *weakSelf = self;
[[PHImageManager defaultManager] requestImageForAsset:lastAsset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFit options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
weakSelf.iconImageView.image = result;
}];
}];