1

ALAssetsLibrary を使用して Photos.app 内の画像を取得できることは知っていますが、Photos.app 内の写真の総数を取得するにはどうすればよいですか?

この質問のコードを使用して Photos.app の最後の画像を取得しているため、写真の数を確認しようとしています。Photos.appから最後の画像を取得しますか?

したがって、デバイスに画像がない場合、上記のリンクのコードは実行されません。

とにかくどうすればこれを手に入れることができますか?

ありがとう!

4

2 に答える 2

6

iOS 8 で導入された新しいPhotosフレームワークでは、以下を使用できますestimatedAssetCount

NSUInteger __block estimatedCount = 0;

PHFetchResult <PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    estimatedCount += collection.estimatedAssetCount;
}];

これにはスマート アルバムは含まれません (そして、私の経験では、有効な「推定カウント」がありません)。代わりに、アセットを取得して実際のカウントを取得できます。

NSUInteger __block count = 0;

// Get smart albums (e.g. "Camera Roll", "Recently Deleted", "Panoramas", "Screenshots", etc.

PHFetchResult <PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    PHFetchResult <PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    count += assets.count;
}];

// Get the standard albums (e.g. those from iTunes, created by apps, etc.), too

collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    PHFetchResult <PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    count += assets.count;
}];

ところで、ライブラリの承認をまだ要求していない場合は、要求する必要があります。たとえば、次のようにします。

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    if (status == PHAuthorizationStatusAuthorized) {
        // insert your image counting logic here
    }
}];

古いAssetsLibraryフレームワークでは、次のことができますenumerateGroupsWithTypes

NSUInteger __block count = 0;

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (!group) {
        // asynchronous counting is done; examine `count` here
    } else {
        count += group.numberOfAssets;
    }
} failureBlock:^(NSError *err) {
    NSLog(@"err=%@", err);
}];

// but don't use `count` here, as the above runs asynchronously
于 2012-09-29T01:41:13.870 に答える
0

使用してenumerateAssetsUsingBlockください。結果が nil ではないたびに、それを配列に追加します (私はそれを と呼びますself.arrayOfAssets)。次に、 nil の結果 (列挙の終点) を取得すると、 get self.arrayOfAssets.count.

編集:さて、これは他の質問のコードにいくつかの変更を加えたものです。使用するenumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:

可変配列を用意して、そこに各画像を配置します。

次に、asset が列挙が終了したことを示す nil の場合、配列をカウントします。

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    self.allPhotos = [[NSMutableArray alloc] init];


    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos                                                                   usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

    // The end of the enumeration is signaled by asset == nil.
    if (alAsset) {
         ALAssetRepresentation *representation = [alAsset defaultRepresentation];
         UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
        [self.allPhotos addObject:latestPhoto];
    if (!asset){
            NSLog:(@"photos count:%d", self.allPhotos.count);
            }
         }
    }];
 }
 failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
  }];
于 2012-09-29T01:41:26.473 に答える