Wednesday, 28 August 2013

ALAssetslibrary - Not able to return to the method the list of informations about images

ALAssetslibrary - Not able to return to the method the list of
informations about images

I have implemented a method that find the images inside the device. For do
this i'm using the ALAssetslibrary framework. I want return an NSString
object with the list of the informations about the images. I have already
found them, but unfortunately the ALAssetslibrary seems that works
asynchronously. This cause a problem. In fact, when i write "return
xmlList" it result null because probably the ALAssetslibrary doesn't have
finish yet to perform the search. How can i resolve this problem? Here
there is my working code.
-(NSString *) getPhotos{
NSMutableArray *idList = [[NSMutableArray alloc] init];
__block XMLWriter* xmlWriter = [[XMLWriter alloc]init];
__block NSString *xmlList;
__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];
}
}];
}
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 writeEndElement:@"Photo"];
}
[xmlWriter writeEndElement:@"Data"];
[xmlWriter writeEndDocument];
xmlList = [xmlWriter toString];
NSLog(@"xml list: %@", xmlList);
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
NSLog(@"xml list2 %@", xmlList);
return xmlList;
}
In my code you can see the line "NSLog(@"xml list: %@", xmlList);" inside
the ""if (group == nil)" block. This return the correct informations in
the log. But the last NSLog "NSLog(@"xml list2 %@", xmlList);" return
null, so it is empty. What can i do? Is there a way for add for example a
timer or something? I don't know how proceed. Thanks

No comments:

Post a Comment