gpt4 book ai didi

ios - 已弃用 ALAssetsLibrary 方法

转载 作者:行者123 更新时间:2023-12-01 17:42:13 24 4
gpt4 key购买 nike

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

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

最佳答案

使用下面的代码从图库中获取所有图片:首先你需要导入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;
}];

关于ios - 已弃用 ALAssetsLibrary 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42555882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com