ALAsset框架在iOS9.0后开始被废弃,取而代之的是iOS8.0版本中推出的PhotoKit框架
1、取出相册资源
用PHAssetCollection fetchAssetCollectionsWithType:subType:options 方法遍历返回PHFetchResult对象,然后获取相册的属性,collection.localizedTitle(相册名称),collection.estimatedAssetCount(相册图片数量)
2、取出相册里的照片
取相册里的图片时,首先要取出PHAsset对象,用PHAsset fetchAssetsInAssetCollection:options 方法遍历返回PHFetchResult对象,然后初始化PHCachingImageManager,在PHCachingImageManager的
requestImageForAsset 方法block中取出相册中的照片,图片的大小由传的参数决定
示例代码:
PHFetchResult *smartCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *collection = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
[smartCollection enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAssetCollection *collection = (PHAssetCollection *)obj;
NSLog(@"smart collection %@ %lu",[obj valueForKey:@"localizedTitle"],(unsigned long)collection.estimatedAssetCount);
}];
[collection enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAssetCollection *collection = (PHAssetCollection *)obj;
NSLog(@"collection %@ %lu",collection.localizedTitle,(unsigned long)collection.estimatedAssetCount);
}];
PHCachingImageManager *manager = [[PHCachingImageManager alloc] init];
PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:collection[0] options:nil];
[assets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[manager requestImageForAsset:obj
targetSize:CGSizeMake(200, 200)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@"image %@",result);
}];
}];