Retrieve only video of particular album from library ios - objective-c

I want the list of video that are stored in particular Album like that(VideoMaker) .
This list is in array of URL.
This list show in uicollectionview
I write this code..for to retrieve the video but it gives null value of URL.
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
NSLog(#"succed");
if (group != NULL) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,BOOL *st){
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
NSLog(#"asset: %#", result);
[assets addObject:result];
}
}];
}
[self.collectionView reloadData];
} failureBlock:^(NSError *error) {
NSLog(#"Failure");
}];
In this code _library is(ALAssetLibrary) and assets is(NSMutableArray)
I created one Album in my Iphone so I want get only this video that is stored in my Album.

I add the assetfilter in this. with the propertyname
The new code is:
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:#"VideoMaker"]) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
[group setAssetsFilter:[ALAssetsFilter allVideos]];
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
NSLog(#"asset: %#", result);
[assets addObject:result];
}
}];
}
[self.collectionView reloadData];
//[self.activity stopAnimating];
//[self.activity setHidden:YES];
}
failureBlock:^(NSError *error){
NSLog(#"failure"); }];
}

Related

How to access smart folder from gallery using ALAsset

I am making an app where I want to get the list of all albums name from gallery including smart folders(favorites, screenshots. this is an old app where we have used ALAsset in order to access the gallery in our app.
Is there any way through which we can access smart folders as well using ALAssetLibrary?
This code is help.
#import <AssetsLibrary/AssetsLibrary.h>
#property (nonatomic, strong) ALAssetsLibrary *_assetsLibrary;
- (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
-(void) getgalleryPic
{
if (self.photos == nil) {
self.photos = [[NSMutableArray alloc] init];
}else
{
[self.photos removeAllObjects];
}
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
// Access has been granted.
NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *library = [self defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset) {
[collector addObject:asset];
}else
{
self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
NSLog(#"photo lenght %lu",(unsigned long)[self.photos count]);
[_collectionVW reloadData];
}
}];
}
failureBlock:^(NSError *error) { NSLog(#"Boom!!!");}
];
}
else if (status == PHAuthorizationStatusDenied) {
// Access has been denied.
}
else if (status == PHAuthorizationStatusNotDetermined) {
// Access has not been determined.
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
// Access has been granted.
NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *library = [self defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset) {
[collector addObject:asset];
}else
{
self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
NSLog(#"photo lenght %lu",(unsigned long)[self.photos count]);
[_collectionVW reloadData];
}
}];
}
failureBlock:^(NSError *error) { NSLog(#"Boom!!!");}
];
}
else {
// Access has been denied.
}
}];
} else if (status == PHAuthorizationStatusRestricted) {
// Restricted access - normally won't happen.
}
}

Get details about videos from album

I am getting all videos from custom album in my app and i need to show those videos into a UICollectionView.
I was able to get the videos to an array and to show the thumbnail of the video ..
How can i get the details of the videos for example: video duration, date of the recording, etc ..
This is how i got the videos from a specific album
assets = [NSMutableArray new];
_library = [ALAssetsLibrary new];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:#"Custom App Album"]) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
[group setAssetsFilter:[ALAssetsFilter allVideos]];
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
NSLog(#"asset: %#", result);
[assets addObject:result];
}
[_collectionView reloadData];
}];
}
} failureBlock:^(NSError *error){
NSLog(#"failure");
}];
And this is how i displayed the thumbnail,
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCell *galleryCell = (GalleryCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"GalleryCell" forIndexPath:indexPath];
ALAsset *alasset = [assets objectAtIndex:indexPath.row];
galleryCell.videoImageView.image = [UIImage imageWithCGImage:alasset.thumbnail];
return galleryCell;
}
Thanks.
Here is the code snippet from React Native. You can get the details from the result and group.
CGSize dimensions = [result defaultRepresentation].dimensions;
CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
[assets addObject:#{
#"node": #{
#"type": [result valueForProperty:ALAssetPropertyType],
#"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
#"image": #{
#"uri": uri,
#"height": #(dimensions.height),
#"width": #(dimensions.width),
#"isStored": #YES,
},
#"timestamp": #(date.timeIntervalSince1970),
#"location": loc ?
#{
#"latitude": #(loc.coordinate.latitude),
#"longitude": #(loc.coordinate.longitude),
#"altitude": #(loc.altitude),
#"heading": #(loc.course),
#"speed": #(loc.speed),
} : #{},
}
}];

Updating PSTCollectionView contents while loading photos

I'm using PSTCollectionView class to load the photos from iPhone camera roll. I want the photos to appear while they're loaded though, not wait until everything is read. My code looks like this:
-(void) loadAssetGroups {
void (^assetGroupEnumerator)
(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
NSLog(#"group....%#",[group valueForProperty:ALAssetsGroupPropertyName]);
[self loadAssetForGroup:group];
}
};
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
NSLog(#"A problem occured. Error: %#", error.localizedDescription);
};
[[ImagePickerViewController defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
}
-(void) loadAssetForGroup:(ALAssetsGroup*)group {
ALAssetsFilter *filter = [ALAssetsFilter allPhotos];
[group setAssetsFilter:filter];
__block NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:0];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result == nil)
{
return;
}
if (![self.allPhotos containsObject:result]) {
[self.assets setValue:result forKey:[result valueForProperty:ALAssetPropertyURLs]];
[self.allPhotos addObject:result];
[indexes addObject:[NSIndexPath indexPathForItem:[self.allPhotos count]-1 inSection:0]];
[self.collectionView performSelectorOnMainThread:#selector(insertItemsAtIndexPaths:) withObject:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:self.allPhotos.count-1 inSection:0]] waitUntilDone:NO];
}
}];
}
From the console I see that collectionView is calling its dataSource method cellForItemAtIndexPath for the first few objects (that would be visible on the screen) but I need to wait for all photos to be read until I see the collectionView refreshed.
Is there a way to have it running more "on the go"?

Get camera roll images and their EXIF data?

I figured out how to get all the user's images in the camera roll using the AssestsLibrary:
- (void)updateLastPhotoThumbnail {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSInteger numberOfAssets = [group numberOfAssets];
if (numberOfAssets > 0) {
NSLog(#"numberOfPictures: %d",numberOfAssets);
//NSInteger lastIndex = numberOfAssets - 1;
int i = 0;
for (i = 0; i <= numberOfAssets-1; i++) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
NSLog(#"theObject!!!! -- (%d) %#",i,thumbnail);
[cameraRollPictures addObject:thumbnail];
}];
}
}
} failureBlock:^(NSError *error) {
NSLog(#"error: %#", error);
}];
}
I successfully created the array of UIIMages, but how could I get EXIF data from the UIImages using this snippet I have?
PS: I've looked at this http://code.google.com/p/iphone-exif, but I cannot get it to build without errors.
You can't get metadata from UIImage objects.
But you can query an ALasset object for metadata:
NSDictionary *metadata = [[result defaultRepresentation] metadata];
Chefs,
Hendrik

ios assets not work on ios5

here is my code:
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
my previous code is this:
assets = [[NSMutableArray alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assets addObject:result];
}
};
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self.tableView reloadData];
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
and with my phone (ios5) doesn't work, can't go to the assetGroupEnumerator...
also in the simulator, the same thing....
any ideas?
thanks in advance
Are you releasing your ALAssetsLibrary before you are done displaying the assets? You need to retain the library throughout the lifetime of the groups and assets you are using.