Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With the iOS Photos Framework how do I list all PHAssetCollections available?

With the iOS Photos Framework how do I list all PHAssetCollections available? I'd like to find the "Photo Roll" collection so that I can retrieve all photos from that collection, specifically. How do I do that with the iOS 8+ using the new PhotosFramework?

like image 507
Andrew Paul Simmons Avatar asked Mar 10 '15 10:03

Andrew Paul Simmons


2 Answers

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

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

In general this is 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);
like image 118
Ryan Heitner Avatar answered Nov 03 '22 19:11

Ryan Heitner


Use below given code snippet to get the 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

like image 6
Mike Clark Avatar answered Nov 03 '22 17:11

Mike Clark