gpt4 book ai didi

ios - Objective-C/ALAssetLibrary - 如何读取和保存有关图像的信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:22 24 4
gpt4 key购买 nike

我使用 ALAssetLibrary 在我的模拟器中找到了一些关于图像的信息。我可以使用 NSLog 正确显示这些信息,但我不知道如何将这些信息保存在列表中。我使用的代码是这样的:

NSMutableArray *list = [[NSMutableArray alloc] init];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSString *description = [asset description];
NSRange first = [description rangeOfString:@"URLs:"];
NSRange second = [description rangeOfString:@"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))];
NSLog(@"Path: %@", path);
[list addObject:path];
NSDictionary *data = [[asset defaultRepresentation] metadata];
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:@"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:@"%@", width];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
NSLog(@"LIST: %@", list);

据我所知,这些操作是异步进行的,所以我不知道如何保存它们。有什么想法吗?

谢谢

最佳答案

为什么不直接创建一个可变数组,并在每次通过枚举时将您关心的值导出到数组中。这样,您将拥有一个易于访问的项目索引列表。这是一个例子:

NSMutableArray *myContainerArray = [NSMutableArray new];

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSString *description = [asset description];
NSRange first = [description rangeOfString:@"URLs:"];
NSRange second = [description rangeOfString:@"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))];
NSLog(@"Path: %@", path);

NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:@"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:@"%@", width];

[myContainerArray addObject:widthString];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

现在请记住,我已经为此示例创建了一个局部变量,您需要创建一个更大范围的变量才能在其他地方访问该数组。然后,您可以使用 [array objectAtIndex:i]; 或现代 Objective C 语法 array[i]; 通过索引访问数组的元素。

关于ios - Objective-C/ALAssetLibrary - 如何读取和保存有关图像的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445237/

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