Accessing Videos in library using AssetsLibrary framework iPhone? - ios

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]];

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");
}];
}

Get All Images from cameraroll asynchrously

I want to get all images from cameraroll asynchrously
Now I am using that code but my application got hanged untill all the images retrieved , I noticed in instigram or other application it doen't take time to load picture from camerroll.
-(void)getAllPictures
{
NSLog(#"i m in ");
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
NSLog(#"i m in block");
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
NSLog(#"result not nill");
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
NSLog(#"url=%#",url);
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
if ([mutableArray count]==count)
{NSLog(#"condition %lu , %i",(unsigned long)mutableArray.count,count);
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSLog(#"image array= %#",imageArray);
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
NSLog(#"group not nill");
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=(int)[group numberOfAssets];
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"There is an error");}];
}
-(void)allPhotosCollected:(NSArray*)imgArray
{
//write your code here after getting all the photos from library...
NSLog(#"all pictures are %#",imgArray);
[_collection_view reloadData];
/*
CGRect collectionFrame = self.collection_view.frame;
collectionFrame.size.height=700;
collectionFrame.size.width=320;*/
self.verticalLayoutConstraint.constant = 700;
//self.collection_view.frame=collectionFrame;
}
Try like this .. Its increasing fast speed. If you will get all images in array then it will crashing your application while using iPhone & iPad that contain 1000+ photos. So Try like this.
library = [[ALAssetsLibrary alloc] init];
if (library == nil)
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
[imageArray addObject:asset];
[collectionViewObj reloadData];
}
}];
}
failureBlock:^(NSError *error)
{
UIAlertView *alertViewObj = [[UIAlertView alloc]initWithTitle:#"Max6Miz does not have access to your photos" message:#"You can enable access in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Settings",#"Ok", nil];
[alertViewObj show];
NSLog(#"error");
}];
Show your image in your delegate function like this using this collectionView cellForItemAtIndexPath
ALAsset *asset = (ALAsset *)imageArray[indexPath.row];
UIImage *showingImage = [UIImage imageWithCGImage:[asset thumbnail]];
Please use new modern Photos framework which is available from iOS 8.0. I've tested it on 50k photos and 1.5k videos stored in hundreds of albums at my own iCloud Photo Library. It works fast and allows to track and animate changes of albums and assets.
Here is nice article to start with Photos framework: https://www.objc.io/issues/21-camera-and-photos/the-photos-framework/

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 to know when block finished enumerating ALAssetsLibrary [duplicate]

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");
}];

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

Resources