2013-08-28 34 views
-1

我已经实施了正确检索有关使用Alassetslibrary图像信息的方法。如你所知,它是异步工​​作的,但在我的情况下,我需要它同步工作。我只需要阅读关于图像的信息并返回到方法。我的应用程序只有一个视图不需要刷新。我如何修改我的方法?此刻行“return xmlList;”结果总是为空,但我知道资产在块中正确读取,因为我已经使用NSlog。的Objective-C/ALAssetslibrary - 我怎样才能改变我的方法工作同步

-(NSString *) getPhotos{ 

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 XMLWriter* xmlWriter = [[XMLWriter alloc]init]; 
__block NSString *xmlList; 
__block NSString *test; 
__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]; 
       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 writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"]; 
     [xmlWriter writeStartElement:@"Data"]; 
     int i = 0; 
     for(i = 0; i<=[idList count]-1; 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]; 
     xmlList = [xmlWriter toString]; 
    } 

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

return xmlList; 

} 

回答

0

好吧,最后我已经解决了我的问题。我使用了NSConditionLock类。这里有更新的代码:

-(NSString *) getPhotos{ 
enum { WDASSETURL_PENDINGREADS = 1, WDASSETURL_ALLFINISHED = 0}; 
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 XMLWriter* xmlWriter = [[XMLWriter alloc]init]; 
__block NSString *xmlList; 
__block NSString *test; 

__block NSConditionLock * assetsReadLock = [[NSConditionLock alloc] initWithCondition:WDASSETURL_PENDINGREADS]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
__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]; 
       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 writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"]; 
     [xmlWriter writeStartElement:@"Data"]; 
     int i = 0; 
     for(i = 0; i<=[idList count]-1; 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]; 
     xmlList = [xmlWriter toString]; 
     [assetsReadLock lock]; 
     [assetsReadLock unlockWithCondition:WDASSETURL_ALLFINISHED]; 
    } 

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

[assetsReadLock lockWhenCondition:WDASSETURL_ALLFINISHED]; 
[assetsReadLock unlock]; 
NSLog(@"XML: %@", xmlList); 
return xmlList; 

}