ALAssetsLibrary方法已被弃用

3

这是已经过时的代码,需要更新为什么样的代码呢?

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];

请查看此链接:https://dev59.com/dVwX5IYBdhLWcg3wtBMa - Anbu.Karthik
1
整个框架已经过时了,请使用 Photos.framework - Larme
1个回答

6
使用以下代码从相册获取所有图片: 首先需要导入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;
                }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接