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

时间:2013-08-26 13:23:22

标签: 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);

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

由于

3 个答案:

答案 0 :(得分:1)

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

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];按索引访问数组的元素。

答案 1 :(得分:1)

最后我使用此代码解决了问题:

NSMutableArray *idList = [[NSMutableArray alloc] init];
NSMutableArray *widthList = [[NSMutableArray alloc] init];
NSMutableArray *heightList = [[NSMutableArray alloc] init];
NSMutableArray *orientationList = [[NSMutableArray alloc] init];
NSMutableArray *dateList = [[NSMutableArray alloc] init];

__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSString *description = [[NSString 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];
                NSLog(@"description %@", 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))];
                [idList addObject:path];

                NSDictionary *data = [[asset defaultRepresentation] metadata];

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

                NSNumber *height = [[[asset defaultRepresentation] metadata] objectForKey:@"PixelHeight"];
                NSString *heightString = [NSString stringWithFormat:@"%@", height];
                [heightList addObject:heightString];

                NSNumber *orientation = [[[asset defaultRepresentation] metadata] objectForKey:@"Orientation"];
                NSString *orientationString = [NSString stringWithFormat:@"%@", orientation];
                if(orientationString != NULL){
                    [orientationList addObject:orientationString];
                } else {
                    NSString *noOrientation = [[NSString alloc] init];
                    noOrientation = @"No orientation avaiable";
                    [dateList addObject:noOrientation];
                }

                NSString *dateString = [[[asset defaultRepresentation] metadata] objectForKey:@"DateTime"];
                if(dateString != NULL){
                    [dateList addObject:dateString];
                } else {
                    NSString *noDate = [[NSString alloc] init];
                    noDate = @"No date avaiable";
                    [dateList addObject:noDate];
                }
            }
        }];
    }
    if (group == nil){
        XMLWriter* xmlWriter = [[XMLWriter alloc]init];
        [xmlWriter writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"];
        [xmlWriter writeStartElement:@"Data"];
        int i = 0;
        for(i = 0; i<=[idList count]-1; i++){
            NSLog(@"width: %@", [widthList objectAtIndex:i]);
            [xmlWriter writeStartElement:@"Photo"];
            [xmlWriter writeAttribute:@"Id" value:[idList objectAtIndex:i]];
            [xmlWriter writeAttribute:@"Width" value:[widthList objectAtIndex:i]];
            [xmlWriter writeAttribute:@"Height" value:[heightList objectAtIndex:i]];
            [xmlWriter writeAttribute:@"Orientation" value:[orientationList objectAtIndex:i]];
            [xmlWriter writeAttribute:@"Date" value:[dateList objectAtIndex:i]];
            [xmlWriter writeEndElement:@"Photo"];
        }
        [xmlWriter writeEndElement:@"Data"];
        [xmlWriter writeEndDocument];
        NSString* xml = [xmlWriter toString];
        NSLog(@"XML: %@", xml);
    }

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

在这种模式下,我还编写了一个xml使用Xml Stream Writer(https://github.com/skjolber/xswi)的信息。 希望这会有所帮助。

答案 2 :(得分:0)

你可以在库块之外创建nsmutablearray的对象,并在块内的数组中添加对象。

-(void)videoList{

RemainingVideo=[[NSMutableArray alloc] initWithCapacity:0];

 ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];


[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop){
     if (group)
     {
         [group setAssetsFilter:[ALAssetsFilter allVideos]];
         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
          {

              NSString *GroupName=[group valueForProperty:ALAssetsGroupPropertyName];

              if ([GroupName isEqualToString:@"waiting"])
              {
                  if (asset)
                  {
                      NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
                      ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
                      NSString *uti = [defaultRepresentation UTI];
                      NSURL  *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
                      NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
                      [dic setValue:title forKey:@"name"];
                      [dic setValue:videoURL forKey:@"url"];

                      [RemainingVideo addObject:dic];

                      if (RemainingVideo.count == 0)
                      {
                          [UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
                      }
                      else
                      {
                          [UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"%d video waiting for upload..",RemainingVideo.count] forState:UIControlStateNormal];
                      }
                  }
              }
              else
              {
                  [UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
              }
          }];

         NSLog(@"total videos:%d",RemainingVideo.count);
     }
     else
     {
         [UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
     }
 }
failureBlock:^(NSError *error)
 {
     NSLog(@"error enumerating AssetLibrary groups %@\n", error);
 }];

}