不推荐使用 ALAssetsLibrary 方法
Posted
技术标签:
【中文标题】不推荐使用 ALAssetsLibrary 方法【英文标题】:ALAssetsLibrary methods deprecated 【发布时间】:2017-03-02 12:43:38 【问题描述】:已弃用,更新后的代码是什么?
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
ALAssetRepresentation *rep = [asset defaultRepresentation];
【问题讨论】:
看到这个***.com/questions/33500266/… 不推荐使用的是整个框架。使用Photos.framework
【参考方案1】:
使用以下代码从图库中获取所有图片: 首先,您需要导入 Photo 框架。
#import <Photos/Photos.h>
获取图片前先授权:
[phphotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
switch (status)
case PHAuthorizationStatusAuthorized:
[self performSelectorOnMainThread:@selector(getAllPictures) withObject:nil waitUntilDone:NO];
// [self getAllPictures];
NSLog(@"PHAuthorizationStatusAuthorized");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"PHAuthorizationStatusRestricted");
break;
case PHAuthorizationStatusDenied:
NSLog(@"PHAuthorizationStatusDenied");
break;
default:
break;
];
-(void)getAllPicture
NSLog(@"Started...");
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.synchronous = YES;
PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
for (PHAsset *asset in result)
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:asset forKey:@"assest"];
[YOUR_ARRAY insertObject:dic atIndex:0];
dic = nil;
NSLog(@"Completed...");
您可以从以下代码中检索图像:
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.synchronous = YES;
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestImageForAsset:YOUR_ARRAY[INDEX_ARRAY][@"assest"]
targetSize:CGSizeMake(self.view.frame.size.width/3, 200)
contentMode:PHImageContentModeDefault
options:requestOptions
resultHandler:^void(UIImage *image, NSDictionary *info)
YOUR_IMAGE_VIEW.image = image;
];
【讨论】:
以上是关于不推荐使用 ALAssetsLibrary 方法的主要内容,如果未能解决你的问题,请参考以下文章
加快从 ALAssetsLibrary 读取和填充 UITableView 的可能方法
ALAssetsLibrary aspect Ratio Thumbnail iOS 4
ALAssetsLibrary.enumerateGroupsWithTypes Swift 中的第一个参数
Xamarin iOS ALAssetsLibrary WriteVideoToSavedPhotosAlbum 不保存到库