2012-09-29 52 views
1

我知道可以通過使用ALAssetsLibrary獲取Photos.app中的圖像,但是如何獲取Photos.app中的照片總數?獲取Photos.app中的圖像數量?

差不多我試圖檢查的照片的數量,因爲我得到的Photos.app最後一個圖像從這個問題的代碼:Get last image from Photos.app?

因此,如果設備上有0的圖像,它贏得從上面的鏈接執行代碼。

無論如何,我怎麼能得到這個?

謝謝!

回答

5

隨着新Photos框架,在iOS 8的推出,您可以使用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 
+0

任何想法如何在新的'照片'框架中做到這一點? – atulkhatri

+0

感謝您的快速編輯。我通過使用PHFetchResult *集合= [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum子類型:PHAssetCollectionSubtypeAlbumRegular選項:無];'這給了我與默認'Photos'應用程序完全相同的結果。 – atulkhatri

+0

@atulkhatri - 非常正確,您必須使用PHAssetCollectionTypeSmartAlbum來獲取智能相冊(例如「相機膠捲」等)。但是,您還必須使用「PHAssetCollectionTypeAlbum」來獲取其他專輯,例如那些從iTunes同步並由應用創建的。看修改後的答案。 – Rob

0

使用enumerateAssetsUsingBlock。每次結果不爲零,將它添加到一個數組(我會稱之爲self.arrayOfAssets)。然後,當你得到一個零結果(枚舉的終點)得到self.arrayOfAssets.count

編輯:好的,所以這裏是來自另一個問題的代碼,幾個變化。使用enumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:

給自己一個可變數組並將每個圖像放在那裏。

然後,當資產沒有信號表明枚舉已完成時,計數數組。

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"); 
    }]; 
+0

我有點理解,但我很困惑。你能把它與我指出的另一個問題聯繫起來嗎? –