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

标签 ios objective-c alassetslibrary

我使用 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/

相关文章:

ios - 在 Swift 中加载插页式广告时出现令人沮丧的内存泄漏

ios - 通过标识符设置表格 View 单元格高度 - Swift

objective-c - 如何确定 NSTimeInterval 是否发生在任意 NSDate 期间?

ios - 我可以像在 Android 中一样通过 ID 获取 View 的实例吗?

iphone - 使用 ALAssetsLibrary 访问单个图像

objective-c - ALAsset URLs 已经从 iOS 4.3 更改为 5.0?

ios - ALAssetsLibraryassetForURL 方法的同步问题

ios - 使用 cocoapods 时,更改 PRODUCT_NAME 的最佳方法是什么?

ios - 核心数据中带有 NSPredicate 的 NSFetchRequest

ios - 如何将图像居中于面部?