1

我目前正在使用ALAssetLibrary检索有关设备(IOS 模拟器)上图像的信息。现在我有了所需的信息,我想把它们写成xml file. 为此,我使用了XmlStreamWriter( https://github.com/skjolber/xswi ) 简单易用。我的问题是EXC_BAD_ACCESS (code=1)运行应用程序时出现错误。我知道它与 相关,streamWriter因为如果我评论这行代码,程序可以完美运行。这是我的代码(我正在使用ARC):

XMLWriter* xmlWriter = [[XMLWriter alloc]init];
[xmlWriter writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"];
[xmlWriter writeStartElement:@"Photos"]; 
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){
                [xmlWriter writeStartElement:@"Photos"];
                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))];
                [xmlWriter writeAttribute:@"id" value:path];
                [xmlWriter writeEndElement:@"Photos"];                    
            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
[xmlWriter writeEndElement];
[xmlWriter writeEndDocument];
NSString* xml = [xmlWriter toString];
NSLog(@"XML: %@", xml);

知道可能是什么问题吗?

我还有一张带有相关错误的图片: 错误图片

谢谢

4

1 回答 1

2

enumerateGroupsWithTypes调用块以异步处理找到的信息。因此,您[xmlWriter writeEndDocument在真正将任何真实内容写入作家之前就打电话了。

您需要更改完成写入操作的方式,以便在块内完成,并且何时groupnil. 在您现有的支票中添加一个else块并放置

[xmlWriter writeEndElement];
[xmlWriter writeEndDocument];
NSString* xml = [xmlWriter toString];
NSLog(@"XML: %@", xml);

在其中(以及您随后所做的任何事情)。

于 2013-08-26T09:57:39.007 回答