Reputation: 595
I have found some informations about images inside my emulator using the ALAssetLibrary. I can show correctly these informations using the NSLog, but i don't know how can i save these informations inside a list for example. The code i'm using is this:
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){
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))];
NSLog(@"Path: %@", path);
[list addObject:path];
NSDictionary *data = [[asset defaultRepresentation] metadata];
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:@"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:@"%@", width];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
NSLog(@"LIST: %@", list);
For what i'm understanding, these operations works asynchronously, so i don't know how can i save them. Any idea?
Thanks
Upvotes: 2
Views: 1636
Reputation: 595
At the end i have resolved the problem using this code:
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 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];
NSLog(@"description %@", 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* xmlWriter = [[XMLWriter alloc]init];
[xmlWriter writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"];
[xmlWriter writeStartElement:@"Data"];
int i = 0;
for(i = 0; i<=[idList count]-1; i++){
NSLog(@"width: %@", [widthList objectAtIndex: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];
NSString* xml = [xmlWriter toString];
NSLog(@"XML: %@", xml);
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
In this mode i have also wrote the informations how an xml using an Xml Stream Writer (https://github.com/skjolber/xswi). Hope that this will help.
Upvotes: 1
Reputation: 969
you can make object of nsmutablearray outside of library block and add object in array inside block.
-(void)videoList{
RemainingVideo=[[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
NSString *GroupName=[group valueForProperty:ALAssetsGroupPropertyName];
if ([GroupName isEqualToString:@"waiting"])
{
if (asset)
{
NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
[dic setValue:title forKey:@"name"];
[dic setValue:videoURL forKey:@"url"];
[RemainingVideo addObject:dic];
if (RemainingVideo.count == 0)
{
[UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
}
else
{
[UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"%d video waiting for upload..",RemainingVideo.count] forState:UIControlStateNormal];
}
}
}
else
{
[UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
}
}];
NSLog(@"total videos:%d",RemainingVideo.count);
}
else
{
[UploadRemainig setTitle:[[NSString alloc] initWithFormat:@"No video found."] forState:UIControlStateNormal];
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
}
Upvotes: 0
Reputation: 130222
Why not just create a mutable array and on each pass through the enumeration export the values you care about to the array. This way, you'll have an indexed list of items that are easily accessible. Here's an example:
NSMutableArray *myContainerArray = [NSMutableArray new];
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){
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))];
NSLog(@"Path: %@", path);
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:@"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:@"%@", width];
[myContainerArray addObject:widthString];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
Now keep in mind, that I've create a local variable for this example, and you'd need to create one of larger scope to be able to access the array elsewhere. Then you can access the elements of the array by index using either [array objectAtIndex:i];
or with the modern Objective C syntax array[i];
.
Upvotes: 1