不建议使用ALAssetsLibrary方法 [英] ALAssetsLibrary methods deprecated

查看:400
本文介绍了不建议使用ALAssetsLibrary方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已弃用,更新后的代码是什么?

This is deprecated, what could be the updated code?

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

推荐答案

使用以下代码从Gallery获取所有图片: 首先,您需要导入Photo框架.

Use below code to get all picture from Gallery: First you need to import Photo framework.

#import <Photos/Photos.h>

获取图像之前先获得授权:

Take Authorization before getting image:

[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...");
}

您可以从下面的代码中检索图像:

You can retrive image from below code:

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方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆