How to get all photos moment wise using ALAssetLibrary in ios7? - ios

I have problem to get photos in moment wise like apple iphone have in ios8. I have implemented for ios8 using PHAsset and Photos.framework. Now, when i run same code in ios7 device then it returns nothing. So, i go with ALAssetLibrary to get photos. Using ALAssetLibrary i also got all photos but that are like albums wise photos. and also using this ALAssetLibrary i cannot get album creation date not its location name, as i have to show this to data on my each section's header.
My code for fetching photos in ios7 using ALAssetLibrary:
_imagearray = [#[] mutableCopy];
__block NSMutableArray *tmpAssets = [#[] mutableCopy];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset!=nil)
{
[tmpAssets addObject:asset];
}
}];
}
self.imagearray = tmpAssets;
NSLog (#“%#“,self.imagearray);
}
failureBlock:^(NSError *error)
{
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
}];

You are out of luck with iOS 7. AssetsLibrary as you observed returns only albums (Camera Roll, user albums). Even though the Photos App in iOS 7 shows Moments, there are no developer APIs in iOS 7 to get Moments.

I found its solutions my self.
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
-(void)loadAssets{
NSMutableArray *unSortArray = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [MomentsVCTR defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil) {
NSLog(#"Done!");
[self manageLocalAssets:unSortArray];
}
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
if (alAsset) {
[unSortArray addObject:alAsset];
}
}];
} failureBlock: ^(NSError *error) {
NSLog(#"No groups: %#",error);
}];
}
-(void)manageLocalAssets:(NSMutableArray*)unSortArray{
NSMutableArray *_resultArray = [[NSMutableArray alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MMM-yyyy"];
NSLog(#"in loadassets");
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[unSortArray sortedArrayUsingDescriptors:descriptors];
for (int k=0; k<reverseOrder.count; k++) {
ALAsset *asset = (ALAsset *)[reverseOrder objectAtIndex:k];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if (![self.arrDate containsObject:dateStr]) {
[self.arrDate addObject:dateStr];
[self.arrEventID addObject:#"0"];
[self.arrEventName addObject:#"0"];
}
[_resultArray addObject:asset];
}
for (int i=0;i<self.arrDate.count;i++) {
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
int tPhoto = 0;
int tVideo = 0;
for (int j=0; j<_resultArray.count; j++) {
ALAsset *asset = (ALAsset*)[_resultArray objectAtIndex:j];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if ([[self.arrDate objectAtIndex:i] isEqualToString:dateStr]) {
UIImage *latestPhotoThumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
[arr addObject:latestPhotoThumbnail];
latestPhotoThumbnail = nil;
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
[arr2 addObject:#"1"];
tVideo++;
}
else{
[arr2 addObject:#"0"];
tPhoto++;
}
NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"dd-MMM-yyyy"];
[self.imageDateArray addObject:[dateFormatter stringFromDate:date]];
[self.imageIdArray addObject:[NSString stringWithFormat:#"%d",i]];
}
}
[self.imagearray addObject:arr];
[self.arrContentType addObject:arr2];
[self.momentArray addObject:[NSString stringWithFormat:#"%lu",(unsigned long)arr.count]];
[self.arrPhotoCount addObject:[NSString stringWithFormat:#"%d",tPhoto]];
[self.arrVideoCount addObject:[NSString stringWithFormat:#"%d",tVideo]];
}
[self setButtonsSize];
self.collection.dataSource = self;
self.collection.delegate = self;
[self.collection reloadData];
[self.collection.collectionViewLayout invalidateLayout];
self.footerView.hidden = TRUE;
self.footerWebView.hidden = TRUE;
}

Related

UItableview loading inside content slow ios

I have an app in which I am filtering the duplicate contacts I have achieved the functionality but the problem is it is quick for few contacts as the contacts are more loading also becomes slow.
Below is the block of code which I am using
-(void)loadDuplicateContacts{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[addressBook loadContacts:^(NSArray<APContact *> * _Nullable contacts, NSError * _Nullable error) {
NSMutableArray *duplucateDataArray = [[NSMutableArray alloc] init];
duplucateDataArray = [contacts mutableCopy];
duplicateContactsArray = [[NSMutableArray alloc] init];
duplicateContactsDictinary = [[NSMutableDictionary alloc] init];
self.dictionaryKeys = [NSArray new];
for (APContact *contact in duplucateDataArray) {
NSMutableArray *countArray = [[NSMutableArray alloc] init];
[duplucateDataArray enumerateObjectsUsingBlock:^(APContact *obj, NSUInteger idx, BOOL *stop)
{
if ([JustNumber([obj.phones[0] number]) isEqualToString:JustNumber([contact.phones[0] number])])
{
[countArray addObject:contact.name.compositeName];
}
}];
if (countArray.count > 1) {
if (duplicateContactsDictinary[JustNumber([contact.phones[0] number])]) {
NSMutableArray *retriveArray = [[NSMutableArray alloc] init];
retriveArray = [duplicateContactsDictinary objectForKey:JustNumber([contact.phones[0] number])];
[retriveArray addObject:contact];
[duplicateContactsDictinary setObject:retriveArray forKey:JustNumber([contact.phones[0] number])];
}else{
NSMutableArray *contactCreationArray = [[NSMutableArray alloc] init];
[contactCreationArray addObject:contact];
[duplicateContactsDictinary setObject:contactCreationArray forKey:JustNumber([contact.phones[0] number])];
}
[duplicateContactsArray addObject:contact];
}
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.tableView reloadData];
});
}];
});
}
Any suggestions would be more helpful

Is there any way to select only image and PDF from gallery or file manager [duplicate]

I want to show all images from device to my app in uicollectionview.
and want to select multiple images from uicollectionview. I have watched numbers of programs.ELCImagePickerController
but i cant get it correctly.
please help me...
thank you
this links works fine...Multi-Select ImagePicker
but how can i get selected images into an array from button Done..
When I press Done button image shown in array like this....
<UIImage: 0x7fca78772510>, {485, 303}
so, how can i get this image in my collection view.. help me guys....
Get all image from gallery
View Controller header(.h) file..
#import <UIKit/UIKit.h>
#include <AssetsLibrary/AssetsLibrary.h>
#interface getPhotoLibViewController : UIViewController
{
ALAssetsLibrary *library;
NSArray *imageArray;
NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
#end
implementation file
declare global count variable as
static int count=0;
#implementation getPhotoLibViewController
-(void)getAllPictures
{
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) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
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];
}
};
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);
}
#end
Use getAllPicture method to get photos from photo library.
OR You can have a look at this blog http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

in iOS How to apply lazy loading while fetching images and videos from assets?

I have fetched the images and videos from gallery, it works fine when the number of videos and images is minimum,but when the number of images and videos is large, it blocks the main thread.
-(void)viewDidAppear:(BOOL)animated{
[NSThread sleepForTimeInterval:1];
// call the same method on a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self getAllVideosAndImages];
});
}
To gat Images and Videos From Asset
-(void)getAllVideosAndImages{
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
assetGroups = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
NSUInteger groupTypes = ALAssetsGroupAll;
void (^assetEnumerator)
( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]||[[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
ALAssetRepresentation *rep = [result defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
//GET DATE
NSDate *myDate = [result valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd,YYYY"];
NSString *realDateStr = [dateFormatter stringFromDate:myDate];
//GET IMG AND VIDEO
UIImage *largeimage = [UIImage imageWithCGImage:iref];
NSData *webData = UIImagePNGRepresentation(largeimage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"img%zd.mov",imgName]];
imgName=imgName+1;
[webData writeToFile:localFilePath atomically:YES];
Para_ImageSet *objImgSet=[Para_ImageSet new];
objImgSet.imagePath=localFilePath;
objImgSet.imageDate=realDateStr;
// [yArrayOfAllImges addObject:objImgSet];
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
objImgSet.is_video=YES;
}
else
{
objImgSet.is_video=NO;
}
if ([tempDate isEqualToString:#""]) {
yArrayOfImagesWithDate=[NSMutableArray new];
[yArrayOfImagesWithDate addObject:objImgSet];
tempDate=realDateStr;
}else
{
//GATE IMAGE COUNT
if ([realDateStr isEqualToString:tempDate]) {
//DO NOTHING
[yArrayOfImagesWithDate addObject:objImgSet];
}
else
{
[yArrayOFNNestedImgs addObject:yArrayOfImagesWithDate];
NSLog(#" ----------ImagesWithDate =%zd, %#",yArrayOfImagesWithDate.count,tempDate);
yArrayOfImagesWithDate=[NSMutableArray new];
[yArrayOfImagesWithDate addObject:objImgSet];
tempDate=realDateStr;
}
}
}
}
}
[self.tableView reloadData];
};
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
// NSNumber *countObj = [NSNumber numberWithInt:count];
NSLog(#" ----------ImagesWithDate =%zd, %#",yArrayOfImagesWithDate.count,tempDate);
[yArrayOFNNestedImgs addObject:yArrayOfImagesWithDate];
NSLog(#" ----------Count =%zd",yArrayOFNNestedImgs.count);
[NSThread sleepForTimeInterval:10];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
};
[library enumerateGroupsWithTypes:groupTypes
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"A problem occurred");}];
}
Please Suggest..Thanks in advance.
[NSThread sleepForTimeInterval:1]; is blocking main thread for a second. It's not needed.
You could use [self performSelectorInBackground:#selector(getAllVideosAndImages) withObject:nil];
Also, wrap first [self.tableView reloadData]; in
dispatch_async(dispatch_get_main_queue(), ^{
...
}); too
In order to lazily load the resources, here is the recommended approach:
Download URLs of all resources and store them into your container (array / objects etc)
Fire a NSURLSessionTask (post iOS 7 only) which runs async on background queue. If you are below iOS 7, you can use NSURLConnection SendAsynchronousRequest API - it's deprecated in iOS 9 so you better get rid of that soon.
Process your downloaded resources while on background queue - store audio/image files, create UIImage from NSData, and so on.
Come back to main queue, then update the relevant UI part with downloaded content. If audio/video, play it. If UIImage, display it.
Here - the entire approach is described in my tutorial.

Get all images from device and show it in uicollectionview

I want to show all images from device to my app in uicollectionview.
and want to select multiple images from uicollectionview. I have watched numbers of programs.ELCImagePickerController
but i cant get it correctly.
please help me...
thank you
this links works fine...Multi-Select ImagePicker
but how can i get selected images into an array from button Done..
When I press Done button image shown in array like this....
<UIImage: 0x7fca78772510>, {485, 303}
so, how can i get this image in my collection view.. help me guys....
Get all image from gallery
View Controller header(.h) file..
#import <UIKit/UIKit.h>
#include <AssetsLibrary/AssetsLibrary.h>
#interface getPhotoLibViewController : UIViewController
{
ALAssetsLibrary *library;
NSArray *imageArray;
NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
#end
implementation file
declare global count variable as
static int count=0;
#implementation getPhotoLibViewController
-(void)getAllPictures
{
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) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
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];
}
};
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);
}
#end
Use getAllPicture method to get photos from photo library.
OR You can have a look at this blog http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

ALAssetsLibrary get Camera Roll

I enumerate all assets groups using ALAssetsLibrary
Here is code:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^enumerate)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
{
// enumerated all albums..
}
// I hot to check if group is Camera Roll ?
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:enumerate
failureBlock:nil];
How to check if some current enumerated is CameraRoll?
Edit: As i tested it was always the last, using this enumerating. But i am not sure if it is the rule, are there any references that i missed?
To get photos from camera roll use ALAssetsGroupSavedPhotos while enumerating assets library:
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:enumerate
failureBlock:nil];
To detect what group you currently get:
if ([[group valueForProperty:#"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos)
{
NSLog(#"Camera roll");
}
imageArray = [[NSArray alloc] init];
NSMutableArray*mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
ALAssetsLibrary*library = [[ALAssetsLibrary alloc] init];
void (^enumerate)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:#"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos)
{
NSLog(#"Camera roll");
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
ALAssetRepresentation *rep = [result defaultRepresentation];
NSLog(#"Asset Name ----> %#",rep.filename);
}];
}
// I hot to check if group is Camera Roll ?
};
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:enumerate
failureBlock:nil];

Resources