【问题标题】:PHAssetCollection for "Camera Roll" and "My Photo Stream" is empty“相机胶卷”和“我的照片流”的 PHAssetCollection 为空
【发布时间】:2017-01-16 07:48:13
【问题描述】:

我试图在我的 iphone 中显示所有专辑标题的列表,当我按下标题时,我希望它在收藏视图中显示该特定专辑的所有照片。我正在使用“PhotosUI”框架,我使用此代码获取专辑标题:

-(void)getLibrariesList
{
    NSArray *collectionsFetchResults;
    albumNames = [[NSMutableArray alloc] init];

    PHFetchResult *smartAlbums = [PHAssetCollection       fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
                                                                                subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                                           subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil];
    PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

    PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
    userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];



    // Add each PHFetchResult to the array
    collectionsFetchResults = @[smartAlbums, userCollections, syncedAlbums];

    for (int i = 0; i < collectionsFetchResults.count; i ++) {

        PHFetchResult *fetchResult = collectionsFetchResults[i];

        for (int x = 0; x < fetchResult.count; x ++) {

            PHCollection *collection = fetchResult[x];

            albumNames[x] = collection.localizedTitle;

        }
    }
    //update the tableview
    [self.libraryTableView reloadData];
}

这段代码通过标题获取特定相册的所有照片:

-(void)showPhotosInCollectionViewForALbumWithName:(NSString*)albumName
{
    libraryAssets = [NSMutableArray array];

    __block PHAssetCollection *collection;

    // Find the album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumName];
    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                          subtype:PHAssetCollectionSubtypeAny
                                                          options:fetchOptions].firstObject;

    PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

    [collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {


        [libraryAssets addObject:asset];
    }];
    [self.libraryPhotosCollectoionView reloadData];
}

对于某些专辑,它可以正常工作,比如“Instagram”之类的专辑和我用手机创建的专辑,但是当我按下“相机胶卷”和“我的照片流”专辑时,它没有显示任何内容。

知道为什么会发生这种情况,或者是否有其他方法可以获取我设备上的所有照片?

【问题讨论】:

    标签: ios objective-c phasset phassetcollection


    【解决方案1】:

    我得到了答案。我检索所有相机胶卷照片和视频。下面是我的代码。

       PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
                                                          subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
                                                          options:fetchOptions].firstObject;
    
    collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    NSLog(@"Custom album images::%@",collectionResult);
    
    
    [collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop)
    {
        NSLog(@"Custom album::%@",asset);
        NSLog(@"Collection Result Count:%lu", (unsigned long)self.collectionResult.count);
    
        //add assets to an array for later use in the uicollectionviewcell
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 2021-09-11
      • 1970-01-01
      • 2012-01-04
      • 2012-11-04
      • 2011-11-26
      • 2011-11-08
      • 1970-01-01
      相关资源
      最近更新 更多