2013-08-26 62 views
1

我正在使用ALAssetLibrary來檢索設備(IOS模擬器)上的圖像信息。 現在我有了所需的信息,我想寫他們像xml file。爲此,我使用簡單易用的XmlStreamWriterhttps://github.com/skjolber/xswi)。我的問題是,當我運行應用程序時,出現EXC_BAD_ACCESS (code=1)錯誤。我知道它與streamWriter有關,因爲如果我評論這段代碼,該程序完美工作。 在這裏有我的代碼(我使用ARC):Objective-C - 使用ALAsset和XmlWriter的EXC_BAD_ACCESS錯誤

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

感謝

+0

什麼是異常的堆棧跟蹤?從例外打印的任何日誌? – Wain

回答

2

enumerateGroupsWithTypes調用塊異步處理找到的信息。所以,在你真正寫入任何真實內容到作者之前,你打電話給[xmlWriter writeEndDocument

您需要更改完成寫入操作的方式,以便在塊內完成並且當groupnil的形式傳遞時。一個else塊添加到您現有的支票,並把

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

在它(不管你做之後)。

+0

感謝您的解釋。你有什麼建議嗎? – Hieicker