不建议使用ALAssetsLibrary方法

编程入门 行业动态 更新时间:2024-10-10 08:24:42
本文介绍了不建议使用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方法

本文发布于:2023-11-24 23:09:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建议   方法   ALAssetsLibrary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!