使用iOS Photos Framework,如何列出所有可用的PHAssetCollections?

时间:2015-03-10 10:25:04

标签: ios objective-c photosframework

使用iOS Photos Framework,如何列出所有可用的PHAssetCollections? 我想找到" Photo Roll"集合,以便我可以从该集合中检索所有照片,特别是。如何使用新的PhotosFramework使用iOS 8+?

2 个答案:

答案 0 :(得分:5)

如果您查看PhotoTypes,您可以看到相机胶卷不包含在PH中,     你可以通过

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

一般来说,这是如何获得一切

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);

答案 1 :(得分:3)

使用以下给定的代码段来获取所有智能相册和所有智能照片

// 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;

       }];
}];

供参考:https://developer.apple.com/library/prerelease/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html