Objective-C - ALAsset和XmlWriter的EXC_BAD_ACCESS错误

时间:2013-08-26 09:35:02

标签: ios objective-c alassetslibrary xmlstreamreader

我正在使用ALAssetLibrary来检索有关设备(IOS模拟器)上的图像的信息。 现在我有了所需的信息,我想把它们写成xml file。为此,我使用简单易用的XmlStreamWriterhttps://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);

知道可能出现什么问题?

我还有一个带有相关错误的图片: Error image

由于

1 个答案:

答案 0 :(得分:2)

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

您需要更改完成写入操作的方式,以便在块内完成,并将group作为nil传递。在现有支票中添加else块并输入

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

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