Get all of the pictures from an iPhone photoLibrary in an array using AssetsLibrary framework?
25,244
Solution 1
//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
Solution 2
Since ALAssetsLibrary
is deprecated now and Photo Framework
is new one. I made my own function in Objective C
to get all photos from Camera Roll and store in NSArray
and displayed in my Collectionview
NSArray *imageArray;
NSMutableArray *mutableArray;
-(void)getAllPhotosFromCamera
{
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = true;
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(@"%d",(int)result.count);
PHImageManager *manager = [PHImageManager defaultManager];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];
// assets contains PHAsset objects.
__block UIImage *ima;
for (PHAsset *asset in result) {
// Do something with the asset
[manager requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:requestOptions
resultHandler:^void(UIImage *image, NSDictionary *info) {
ima = image;
[images addObject:ima];
}];
}
imageArray = [images copy]; // You can direct use NSMutuable Array images
}
Author by
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
I want to get all of the pictures from photoLibrary. I would prefer a method or example that I can use directly.
-
Ankit Malhotra over 10 yearsThe memory footprint of above approach is tremendous ! Although it is a very apt & simple way of doing it. Memory management specifically becomes difficult in this due to use of asynchronous block.
-
Harjot Singh over 10 yearsIt gives error, if we use "#include <AssetsLibrary/AssetsLibrary.h> " So please use this in your header to remover any errors: #import <AssetsLibrary/ALAsset.h> For Help: stackoverflow.com/questions/10342931/…
-
Harjot Singh over 10 yearsImport #import <AssetsLibrary/ALAssetRepresentation.h> For over come this error: Receiver type 'ALAssetRepresentation' for instance message is a forward declaration
-
C0mrade over 8 yearsIf you load images more then 60, Memory goes up to 500MB. Tested on ios 9.1 with iphone 6S. will post improved answer.
-
locomotion over 8 yearsFor reducing memory consumption, you can get image thumbnails using [asset thumbnail] or [asset aspectRatioThumbnail] instead of [[asset defaultRepresentation] fullScreenImage]]. You can save image URLs and get full versions of images later on demand.
-
Underdog over 8 yearsto save memory save the ALAsset, then only fetch the image/thumbnail once you will dispaly it, or once the cell is being shown...
-
karan over 8 yearsAssetsLibrary is deprecated... Can we have new PHPhotoLibrary example.
-
karan over 8 yearsI added new version of getting photos, since AssetsLibrary was deprecated.
-
Sneha almost 8 yearsits deprecated now .... !!! can you provide use of PHPhotoLibrary instead...?????
-
Gowri G about 4 yearsgetting crash like [PHPhotoLibrary init[ unsupported.
-
Gowri G about 4 yearsIs there any other option to resolve this one. please let me know?