With the iOS Photos Framework, how can I display all the available PHAssetCollections?

With the iOS Photos Framework, how can I display all the available PHAssetCollections? I would like to find the β€œPhoto Roll” collection so that I can, in particular, get all the photos from this collection. How to do it using iOS 8+ using the new PhotosFramework?

+5
source share
2 answers

If you look at PhotoTypes, you will see that Camera Roll is not included in PH, you can get to it

PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil]; PHAssetCollection *assetCollection = result.firstObject; NSLog(@"%@", assetCollection.localizedTitle); // Camera Roll 

In general, how to get everything

 PHFetchOptions *options = [[PHFetchOptions alloc] init]; options.wantsIncrementalChangeDetails = YES; options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d",PHAssetMediaTypeImage]; PHFetchResult *albums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; for (PHAssetCollection *sub in albums) { PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:sub options:options]; } #pragma mark - PHAssetCollection types typedef NS_ENUM(NSInteger, PHAssetCollectionType) { PHAssetCollectionTypeAlbum = 1, PHAssetCollectionTypeSmartAlbum = 2, PHAssetCollectionTypeMoment = 3, } NS_ENUM_AVAILABLE_IOS(8_0); typedef NS_ENUM(NSInteger, PHAssetCollectionSubtype) { // PHAssetCollectionTypeAlbum regular subtypes PHAssetCollectionSubtypeAlbumRegular = 2, PHAssetCollectionSubtypeAlbumSyncedEvent = 3, PHAssetCollectionSubtypeAlbumSyncedFaces = 4, PHAssetCollectionSubtypeAlbumSyncedAlbum = 5, PHAssetCollectionSubtypeAlbumImported = 6, // PHAssetCollectionTypeAlbum shared subtypes PHAssetCollectionSubtypeAlbumMyPhotoStream = 100, PHAssetCollectionSubtypeAlbumCloudShared = 101, // PHAssetCollectionTypeSmartAlbum subtypes PHAssetCollectionSubtypeSmartAlbumGeneric = 200, PHAssetCollectionSubtypeSmartAlbumPanoramas = 201, PHAssetCollectionSubtypeSmartAlbumVideos = 202, PHAssetCollectionSubtypeSmartAlbumFavorites = 203, PHAssetCollectionSubtypeSmartAlbumTimelapses = 204, PHAssetCollectionSubtypeSmartAlbumAllHidden = 205, PHAssetCollectionSubtypeSmartAlbumRecentlyAdded = 206, PHAssetCollectionSubtypeSmartAlbumBursts = 207, PHAssetCollectionSubtypeSmartAlbumSlomoVideos = 208, PHAssetCollectionSubtypeSmartAlbumUserLibrary = 209, // Used for fetching, if you don't care about the exact subtype PHAssetCollectionSubtypeAny = NSIntegerMax } NS_ENUM_AVAILABLE_IOS(8_0); 
+5
source

Use the code snippet below to get all smart albums and all smart photos

 // Get all Smart Albums PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; [smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) { NSLog(@"album title %@", collection.localizedTitle); }]; // Get all photos PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; // Get assets from the PHFetchResult object [allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { NSLog(@"asset %@", asset); CGSize size=CGSizeMake(90, 90); PHImageManager *imageManager; [imageManager requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage *result, NSDictionary *info) { yourImageView.image=result; }]; }]; 

for reference: https://developer.apple.com/library/prerelease/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

+3
source

Source: https://habr.com/ru/post/1215042/


All Articles