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.
Related
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 :)
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];
}];
I am trying to retrieve all photo album names and all photos with that album name. The album names I got from [group valueForProperty:ALAssetsGroupPropertyName] are mostly correct but there was one album name that says (null) and the album called Favorites does not show.
Album names array:
((null), Camera Roll, Last Import, Nissan Juke, Cam)
Here's my 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)
{
[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]]];
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 would like to know why there is a (null) album name and why the album name Favorites does not show.
Thanks.
When the enumeration is done, enumerationBlock is invoked with group set to nil, so you should check each time the group is nil or not and execute reloadData only on the enumeration is done. The proper logic should look like this:
[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);
}];
For the Favorites, it's a new feature in PhotoKit for iOS8:
With iOS 8, Apple has given us PhotoKit, a modern framework that’s more performant than AssetsLibrary and provides features that allow applications to work seamlessly with a device’s photo library.
Favorite and Hidden Assets
To find out if an asset was marked as favorite or was hidden by the user, just inspect the favorite and hidden properties of the PHAsset instance.
See also: The Photos Framework
So you have to show the Favorites via the PhotoKit framework, check the official sample project: ExampleappusingPhotosframework.zip.
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
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
}];