IOS 8 Photos Basics: Get a list of all albums with iOS8

How do I get a list of all the collections, including the camera roll (now called the moment), in iOS8?

In iOS 7, I use the ALAssetGroup enumeration block, but this does not include iOS moments that seem to be equivalent to Camera Roll in iOS7.

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) { if (group == nil) {// We're done enumerating return; } [group setAssetsFilter:[ALAssetsFilter allAssets]]; if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) { [_assetGroups insertObject:group atIndex:0]; } else { [_assetGroups addObject:group]; } }; // Group Enumerator Failure Block void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) { SMELog(@"Enumeration occured %@", [error description]); }; // Enumerate Albums [_library enumerateGroupsWithTypes:kSupportedALAlbumsMask usingBlock:assetGroupEnumerator failureBlock:assetGroupEnumberatorFailure]; }]; 
+42
ios8
Sep 22 '14 at 19:04
source share
3 answers

Using the Photos Framework is a little different, you can achieve the same result, but you just need to do it in parts.

1) Get all the photos (Moments in iOS8 or Camera Roll before)

 PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 

Optionally, if you want them to be sorted by creation date, you simply add PHFetchOptions like this:

 PHFetchOptions *allPhotosOptions = [PHFetchOptions new]; allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions]; 

Now, if you want, you can get assets from the PHFetchResult object:

 [allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { NSLog(@"asset %@", asset); }]; 

2) Get all user albums (with an additional view, for example, to show only albums with at least one photo)

 PHFetchOptions *userAlbumsOptions = [PHFetchOptions new]; userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"]; PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions]; [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) { NSLog(@"album title %@", collection.localizedTitle); }]; 

For each PHAssetCollection returned from PHFetchResult *userAlbums , you can choose PHAssets like this (you can even limit the results to only photo actions):

 PHFetchOptions *onlyImagesOptions = [PHFetchOptions new]; onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage]; PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions]; 

3) Get smart albums

 PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; [smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) { NSLog(@"album title %@", collection.localizedTitle); }]; 

In smart albums, it should be noted that collection.estimatedAssetCount can return NSNotFound if the AssetCount score cannot be determined. As the name implies, this is rated. If you want to be sure of the number of assets, you must select and get the value count:

 PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil]; 

number of assets = assetsFetchResult.count

Apple has an example project that does what you want:

https://developer.apple.com/library/content/samplecode/UsingPhotosFramework/ExampleappusingPhotosframework.zip (you must be a registered developer for this)

+117
Oct 05 '14 at 10:17
source share

This is just a translation of @Ladislav's excellent accepted answer to Swift:

 // *** 1 *** // Get all photos (Moments in iOS8, or Camera Roll before) // Optionally if you want them ordered as by creation date, you just add PHFetchOptions like so: let allPhotosOptions = PHFetchOptions() allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] let allPhotosResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: allPhotosOptions) // Now if you want you can get assets from the PHFetchResult object: allPhotosResult.enumerateObjectsUsingBlock({ print("Asset \($0.0)") }) // *** 2 *** // Get all user albums (with additional sort for example to only show albums with at least one photo) let userAlbumsOptions = PHFetchOptions() userAlbumsOptions.predicate = NSPredicate(format: "estimatedAssetCount > 0") let userAlbums = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: userAlbumsOptions) userAlbums.enumerateObjectsUsingBlock( { if let collection = $0.0 as? PHAssetCollection { print("album title: \(collection.localizedTitle)") //For each PHAssetCollection that is returned from userAlbums: PHFetchResult you can fetch PHAssets like so (you can even limit results to include only photo assets): let onlyImagesOptions = PHFetchOptions() onlyImagesOptions.predicate = NSPredicate(format: "mediaType = %i", PHAssetMediaType.Image.rawValue) if let result = PHAsset.fetchKeyAssetsInAssetCollection(collection, options: onlyImagesOptions) { print("Images count: \(result.count)") } } } ) // *** 3 *** // Get smart albums let smartAlbums = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .AlbumRegular, options: nil) // Here you can specify Photostream, etc. as PHAssetCollectionSubtype.xxx smartAlbums.enumerateObjectsUsingBlock( { if let assetCollection = $0.0 as? PHAssetCollection { print("album title: \(assetCollection.localizedTitle)") // One thing to note with Smart Albums is that collection.estimatedAssetCount can return NSNotFound if estimatedAssetCount cannot be determined. As title suggest this is estimated. If you want to be sure of number of assets you have to perform fetch and get the count like: let assetsFetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: nil) let numberOfAssets = assetsFetchResult.count let estimatedCount = (assetCollection.estimatedAssetCount == NSNotFound) ? -1 : assetCollection.estimatedAssetCount print("Assets count: \(numberOfAssets), estimate: \(estimatedCount)") } } ) 
+15
Jan 29 '16 at 10:28
source share

Try this code ...

self.imageArray = [[NSArray 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:countValue]; // 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]; }]; self.imageArray = [images copy]; 
0
Sep 05 '17 at 13:19 on
source share



All Articles