2

extracting image from Photolibrary we use the UIImagePickerViewController and UIImagePickerControllerSourceTypePhotoLibrary to go into Photolibrary and get image, Now here my question How do i get Count of images which are saved in my photoLibary.

Is its allowed me get images count to show in my application.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
kiran
  • 4,285
  • 7
  • 53
  • 98

3 Answers3

7

Yes it is allowed you can fetch all image using below method:- import Photos Framework

#import <Photos/Photos.h>


-(void)getAllPhotosFromCamera
{
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];

PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode   = PHImageRequestOptionsResizeModeFast;
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];
[_cView reloadData];
}
Viraj Padsala
  • 1,388
  • 1
  • 13
  • 31
2

If you want to get the count then with the Photos Framework you can get the count like this

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(@"count of All Photos from Moments in iOS8, or Camera Roll - %lu",(unsigned long)allPhotosResult.count);

also don't forget to import Photos Framework

#import <Photos/Photos.h>
Rajat
  • 10,977
  • 3
  • 38
  • 55
0

Here you can get all image count. It includes all Album and Camera Roll images too.

__block NSInteger intTotalCount=0;
// Get all Album list...
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx1, BOOL *stop) {
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    intTotalCount+=assetsFetchResult.count;
}];

// Get image count from Camera Roll
PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d ",PHAssetMediaTypeImage];
PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
intTotalCount+=allPhotosResult.count;

NSLog(@"%d",intTotalCount);
kb920
  • 3,039
  • 2
  • 33
  • 44