How find the album name for each image using the ALAssetsLibrary - ios

I’m using the AlAssetLibrary class for retrieve informations about the images inside my iPad. As you can see actually i have found the pixel width of the asset. What i need to find now is the name of the album for each asset. So if all assets are in the “camera” album i need to find it for each asset. How can i proceed?
Here there is my code. Please note the NSString assetAlbumName. It is returning to me an error.
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]]; //search for the photos
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:#"PixelWidth"]; //find the key with "PixelWidth" name
NSString *widthString = [NSString stringWithFormat:#"%#", width]; //take the value of the key
NSString *assetAlbumName = [asset valueForProperty:ALAssetsGroupPropertyName]; //it return to me an ALErrorInvalidProperty
}
}
}
}
Thanks

you may try
NSString *albumName = [group valueForProperty:ALAssetsGroupPropertyName];
you are doing
NSString *albumName = [assets valueForProperty:ALAssetsGroupPropertyName];

*// emumerate through our groups and only add groups that contain photos*
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[group setAssetsFilter:onlyPhotosFilter];
if ([group numberOfAssets] > 0)
{
[self.groups addObject:group]; //groups is NSMutableArray
}
else
{
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
};
// enumerate only photos
NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
[self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:failureBlock];
ALAssetsGroup *groupAsset = groups[objectIndex];
NSString *albumName = [groupAsset valueForProperty:ALAssetsGroupPropertyName];
Here is apple's sample code, will help you more..
https://developer.apple.com/library/ios/samplecode/MyImagePicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010135

Related

iOS - Get Camera Roll images from ALAssetsLibrary

I managed to get photos from **ALAssetsLibrary** with this code:
-(void)getPhotosFromAssetsLibWithPhotoFilter:(NSString *)filterAlbumString
{
_assets = [#[] mutableCopy];
__block NSMutableArray *tmpAssets = [#[] mutableCopy];
__block NSMutableArray *albumGroup = [#[] mutableCopy];
ALAssetsLibrary *assetsLibrary = [PhotoLibViewController defaultAssetsLibrary];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result)
{
if (![filterAlbumString isEqualToString:#""])
{
if ([[NSString stringWithFormat:#"%#", [group valueForProperty:ALAssetsGroupPropertyName]] isEqualToString:filterAlbumString])
{
[tmpAssets addObject:result];
}
}
else
{
[tmpAssets addObject:result];
}
}
}];
[albumGroup addObject:[NSString stringWithFormat:#"%#", [group valueForProperty:ALAssetsGroupPropertyName]]];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
if ([self respondsToSelector:#selector(retrievedPhotoLibrary:)])
{
NSArray *albumGroupReversed = [[albumGroup reverseObjectEnumerator] allObjects];
[self retrievedPhotoLibrary:albumGroupReversed];
}
});
self.assets = [[tmpAssets reverseObjectEnumerator] allObjects];
[self.collectionView reloadData];
}
} failureBlock:^(NSError *error) {
NSLog(#"Error loading images %#", error);
}];
}
I use it like this:
[self getPhotosFromAssetsLibWithPhotoFilter:#"Camera Roll"];
This works great. But the problem is I am localising my app and other languages that is not English does not use "Camera Roll" as the name of the album. I get no images when I use #"Camera Roll".
Is there a name to use that represents Camera Roll ? That will work on every device no matter the language?
Please use ALAssetsGroupSavedPhotos to filter.
Try something like this,
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
// .. do something with the asset
}
}
];
}
failureBlock:^(NSError *error)
{
// User did not allow access to library
//.. handle error
}
];
you can use different source by replacing enumerateGroupsWithTypes.
Second thing ALassetlibrary is deprecated now so you should try PHPhotoLibrary.
Hope this will help :)

How to get the photos according to date in objective c

I am using ALAssetsLibrary to get the photos and display them. But i want the latest picture to be at front(which means sorted according to date-time).Is this possible using ALAssetsLibrary ?? If not what could be the possible way to achieve that??
ALAssetsLibrary *al = [AssetManager defaultAssetsLibrary];
[al enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
[self.assets addObject:asset];
}
}
];
NSLog(#"first %#",self.assets);
self.assetsR=[[[self.assets reverseObjectEnumerator] allObjects] mutableCopy];
[self.collectionData reloadData];
You can use ELCImagePickerController class
it has onOrder property
Try the following
self.assetsR = [self.assets sortedArrayUsingComparator:^NSComparisonResult(ALAsset *first, ALAsset *second) {
NSDate * date1 = [first valueForProperty:ALAssetPropertyDate];
NSDate * date2 = [second valueForProperty:ALAssetPropertyDate];
return [date2 compare:date1];
}];

get an array of Photos in ios Photo Library query by day

I want when I input day. Does iOS have any property to know or which function for us to get all Photos in iOS Library on that day? Or we must call for loop with all photos to get which photos in that day?
- (void)getAllPhotosInCameraRoll {
NSMutableArray *temp = [NSMutableArray array];
[self.defaultLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
NSMutableDictionary *dicPhoto = [NSMutableDictionary dictionary];
[dicPhoto setObject:result forKey:#"alassert"];
[dicPhoto setObject:#1 forKey:#"isHidden"];
[temp addObject:dicPhoto];
}
}];
self.arrayLibraryPhotos = temp;
[self.photosCollection reloadData];
}
} failureBlock:^(NSError *error) {
}];}
You can use NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate]; and then just query or group it the way you want to in your app.

Get counts of photos and videos in ALAssetGroup

I enumerate my ALAssetGroup like this:
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
if (group.isEditable){
NSLog(#"group is %#", group);
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[groups addObject:group];
}
This filters the group to have only photos included in it's .numberOfAssets. However, I'd like to get both the photos count and the video count. How would I do that without enumerating the whole thing for the 2nd time?
ALAssetsLibrary *al = [[ALAssetsLibrary alloc]init];
[al enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName]isEqualToString:#"MyAlbumName"]) {
NSLog(#"in album");
int nrAssets=[group numberOfAssets];
__block int countVideo;
__block int countPhoto;
countPhoto=countVideo=0;
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if ([[asset valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypeVideo]) {
NSLog(#"eVideo ... count++");
countVideo++;
}
else if(asset valueForProperty:ALAssetPropertyType]isEqualToString:AlAssetTypeVideo]){
NSLog(#"EPhoto ... ");
countPhoto++;
}
}];
}
}
failureBlock:^(NSError *error) { NSLog(#"Boom!!!");}
];
i use this code for a specific album, but you can modify for all albums, hope it helped you :)
The code block below counts all videos and photos:
__block int videoCount = 0;
__block int photoCount = 0;
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init];
[assetLibrary
enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil) {
// enumeration complete
return;
}
int total = group.numberOfAssets;
[group setAssetsFilter:[ALAssetsFilter allVideos]];
int groupVideoCount = group.numberOfAssets;
videoCount += groupVideoCount;
photoCount += total - groupVideoCount;
}
failureBlock:^(NSError *error) {
// Handle error
}];

Accessing the image gallery through code for displaying in image view

I've just started a new project where I want the user to be able to pick one of the images in the devices gallery.
I am trying to achieve this by using an ImageView and a UIStepper.
I want to write all images inside the gallery into an array and have the imageView navigate through the array with the + and - buttons of the stepper (selecting the current array position +1 or -1 depending on click).
OK as per prior discussion, here is the project: AssetLibraryPhotosViewer
Have not done an extensive testing, though does seem to run OK both on simulator and real device
#Exothug, to give you an idea of how to enumerate the device library accessing full screen photos:
ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group enumerateAssetsUsingBlock:^(ALAsset* asset, NSUInteger index, BOOL* innerstop) {
if (asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
if (iref) {
UIImage *image = [UIImage imageWithCGImage:iref scale:rep.scale
orientation:(UIImageOrientation)rep.orientation];
// process the image here
}
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"failure: %#", [error localizedDescription]);
}];
you can just process the image via adding it to your array, however depending on number of images in the library it might not be most effective. an alternative approach would be using images URL / indexes to iterate through the library, fetching the image from the library as its needed for display in your ImageView
Maybe try something like this, choose the directory if you want a specific group of images.
NSMutableArray *result = [NSMutableArray array];
[[[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
NSString *path = [obj lastPathComponent];
[result addObject:path];
];
I thought that , You just need to retrieve the Photos of Camera Roll from Your device .
If so , Try with this :
ALAssetsLibrary
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
OR
//Get camera roll images
- (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);
}];

Resources