2012-06-20 106 views
0

我想獲取photoLibrary中的照片數量。目前,我可以從照片庫中獲取照片並將其添加到myApp的文檔指導方針中。但我想要的是,將所有照片從photoLibrary保存到myApp ALL ATONCE的文檔指引中。這就是爲什麼我需要照片庫中的照片數量。我用UIImagePickerControllerSourceTypePhotoLibrary從iPhone photoLibrary中檢索照片。如何從iphone照片庫中獲取照片總數。

任何幫助將appreaciated ..

在此先感謝....

+0

http://stackoverflow.com/questions/5403771/iphoneprogrammatically-find-the-count-of-videos-and-images-from-ph oto-album – san

回答

1

使用ALAssetsLibrary此:

int imgCount = 0; 
self.assetsLibrary = [[ALAssetsLibrary alloc] init]; 
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(dispatchQueue, ^(void) { 
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, 
    __block BOOL foundThePhoto = NO; 
if (foundThePhoto){ *stop = YES; 
} 
BOOL *stop) { 
/* Get the asset type */ 
NSString *assetType = [result valueForProperty:ALAssetPropertyType]; 
if ([assetType isEqualToString:ALAssetTypePhoto]){ NSLog(@"This is a photo asset"); 
foundThePhoto = YES; *stop = YES; 
/* Get the asset's representation object */ 
ALAssetRepresentation *assetRepresentation = [result defaultRepresentation]; 
/* We need the scale and orientation to be able to construct a properly oriented and scaled UIImage out of the representation object */ 
CGFloat imageScale = [assetRepresentation scale]; 
UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation]; 
dispatch_async(dispatch_get_main_queue(), ^(void) { 
CGImageRef imageReference = [assetRepresentation fullResolutionImage]; 
/* Construct the image now */ 
UIImage *image = [[UIImage alloc] initWithCGImage:imageReference 
scale:imageScale orientation:imageOrientation]; 
//Write image to doument directory 
imgCount ++; 
} 
}); 
} failureBlock:^(NSError *error) { 
} }]; 
NSLog(@"Failed to enumerate the asset groups."); }]; 
}) 

NSLog(@"Total Image Count %d",imgCount); 
0

低於計數的代碼塊中的所有視頻和照片:

__block int videoCount = 0; 
__block int photoCount = 0; 
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init]; 
[assetLibrary 
enumerateGroupsWithTypes:ALAssetsGroupAll 
usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
    if (group == nil) { 
     // enumeration complete 
     return; 
    } 
    int total = group.numberOfAssets; 
    [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
    int groupVideoCount = group.numberOfAssets; 
    videoCount += groupVideoCount; 
    photoCount += total - groupVideoCount; 
} 
failureBlock:^(NSError *error) { 
    // Handle error 
}]; 
+0

謝謝你的答覆..但我已經得到了答案.. –

相關問題