how to know when block finished enumerating ALAssetsLibrary [duplicate] - ios

This question already has answers here:
ALAssets groups enumeration: how to check for completion?
(2 answers)
Closed 8 years ago.
Im enumerating the ALAssetsLibrary to know what groups my photo library have,
How can I know when the block stopped enumerating?
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(#"tu arraya :: %#", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop){
// ---> ALAsset:
NSLog(#" Stop? %#", (stop ? #"YES" : #"NO") );
}];
}
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
NSLog(#"tu arraya 1:: %#", albumsArray);

Check if (group == nil) after the enumerateAssetsUsingBlock statement.
See below:
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(#"tu arraya :: %#", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop){
// ---> ALAsset:
NSLog(#" Stop? %#", (stop ? #"YES" : #"NO") );
}];
// this is the end of enumeration
if (group == nil)
{
NSLog(#"THE END!!!");
}
}
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
NSLog(#"tu arraya 1:: %#", albumsArray);
Also, you should really think about breaking up your blocks to make things easier to read. For example:
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [BMBAssetManager defaultAssetsLibrary];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) =
^(ALAsset *result, NSUInteger index, BOOL *stop)
{
// ---> ALAsset:
NSLog(#" Stop? %#", (stop ? #"YES" : #"NO") );
};
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) =
^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(#"tu arraya :: %#", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock:assetEnumerator];
// this is the end of enumeration
if (group == nil)
{
NSLog(#"THE END!!!");
}
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];

Related

how to display images by using ALAsseetLibrary

I am accessing the list of images by using the ALAssetLibrary and i got the list of all image in log file . But i am unable to display any image. Can anyone help me through this?
For getting the list of all images my code is here---
- (IBAction)getListOfImages:(id)sender {
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
NSMutableArray *xy =[[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSLog(#"result is:%#",result);
NSLog(#"asset URLDictionary is:%#",assetURLDictionaries);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
NSLog(#" xy is:%#",xy);
UIImageView *image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
NSLog(#"image is:%#",image);
}
failureBlock:^(NSError *error){ NSLog(#"test:Fail"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
NSLog(#"hello");
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
NSLog(#"Number of assets in group :%d",[group numberOfAssets]);
NSLog(#"asset group is:%#",assetGroups);
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(#"A problem occurred");
}];
}

NSAssetLibrary works on simulator but not on device

I want to fetch all the images from photo library I'm writing. Following code works on simulator but not on device. Please help me.Thanks in advance.
-(void)getImagesFromDevice
{
arrOfImages = [[NSMutableArray alloc]init];
if (_isForAvtars)
{
}
else
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];//ALAssetsGroupAll
NSLog(#"Authirization status:%ld",(long)[ALAssetsLibrary authorizationStatus]);
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group != nil)
{
NSLog(#"Gruop is not nil");
NSLog(#"name %#",[group valueForProperty:ALAssetsGroupPropertyName]);
int a = (int)[group numberOfAssets];
NSLog(#"%d",a);
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result == nil)
{
return;
}
UIImage *img = [UIImage imageWithCGImage:[[result defaultRepresentation] fullScreenImage] scale:1.0 orientation:(UIImageOrientation)[[result valueForProperty:#"ALAssetPropertyOrientation"] intValue]];
if (img != nil)
{
NSLog(#"Add image");
[arrOfImages addObject:img];
}
}];
}
else
{
NSLog(#"Gruop is nil");
[collectionviewPhotosVideos reloadData];
}
}
failureBlock: ^(NSError *error)
{
NSLog(#"No groups");
}
];
}

ALAssetsLibrary return two duplicated images (possible second one is HDR) with different URL

I get a list of images by code bellow, but system return two duplicates with different URI. How I can avoid duplicates there?
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
if (!_library) _library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
NSDate * date = [result valueForProperty:ALAssetPropertyDate];
if (url && date) [_imagesArray addObject:#{#"url":url,#"date":date}];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[group numberOfAssets];
} else {
[_imagesArray sortUsingDescriptors:#[[[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO]]];
}
};
[_library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"There is an error");}];

How do i get only the videos using ALAssetsLibrary

I am trying to get videos present from the photo library from the following piece of code.But i also get images list.How do i get list of all videos? what am i doing wrong?
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
xy =[[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSLog(#"result is:%#",result);
NSLog(#"asset URLDictionary is:%#",assetURLDictionaries);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
NSLog(#" xy is:%#",xy);
image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
NSLog(#"image is:%#",image);
}
failureBlock:^(NSError *error){ NSLog(#"test:Fail"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
NSLog(#"hello");
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
NSLog(#"Number of assets in group :%d",[group numberOfAssets]);
NSLog(#"asset group is:%#",assetGroups);
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"A problem occurred");}];
You need to add an ALAssetsFilter to the group during enumeration. Here's a basic example:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSDictionary *meta = [[asset defaultRepresentation] metadata];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
}];
For future reference, the available filters are:
- allPhotos
- allVideos
- allAssets

Accessing Videos in library using AssetsLibrary framework iPhone?

I am trying to access videos in iPhone library using AssetsLibrary Framework with the help of following code...but when I run the application the code is not working...the array assets is still empty? What am I doing wrong?
by the way my iPhone is a 3G upgraded to iPhone 4.1.(but assets framework is not giving any error)
NSMutableArray *assets = [[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assets addObject:result];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {NSLog(#"dont See Asset: ");
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
the 4.1 upgrade seems to have 'broken' some of the image picker examples, observe the following lines, which run after the code you have posted:
NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces;
[assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:failureBlock];
This gives zero assets both on my 4.1 iPhone and simulator. However changing the groupTypes helps, so set
NSUInteger groupTypes = ALAssetsGroupAll;
and you should start seeing some assets.
I did this in Xcode 4.1, IOS 5:
ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assets addObject:result];
}
};
ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {NSLog(#"dont See Asset: ");
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
It worked for me.
Double Init:
NSMutableArray *assets = [[NSMutableArray alloc]init];
ALAssetsLibrary *library =[[ALAssetsLibrary alloc]init];
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assets addObject:result];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {NSLog(#"dont See Asset: ");
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init**];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
I am using ELC Image Picker Controller, and it's already using ALAssetsGroupAll and still not showing any videos.
I solved this by adding an asset filter. You can limit to videos, photos or both. I guess photos-only was the default. Please consider the following line if the above fails:
[assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];

Resources