簡體   English   中英

“相機膠卷”和“我的照片流”的PHAssetCollection為空

[英]PHAssetCollection for “Camera Roll” and “My Photo Stream” is empty

我試圖顯示我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”和我用手機創建的專輯,但是當我按“ Camera Roll”和“ My Photo Stream”專輯時,它什么也沒顯示。

有什么想法為什么會發生這種情況,或者是否還有另一種方法來獲取我設備的所有照片?

我得到了答案。 我檢索所有相機膠卷照片和視頻。 下面是我的代碼。

   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
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM