how to get total count of photos from iphone photoLibrary.?

江枫思渺然 提交于 2019-12-25 05:04:30

问题


i want to get the count of photos in photoLibrary. Currently, i'am able to get the photoes from photoLibrary and add to myApp's Document directry ONE BY ONE. But what i want is, save all the photos from photoLibrary to Document directry of myApp ALL AT ONCE. Thats why i need the count of photoes in photoLibrary. I've used UIImagePickerControllerSourceTypePhotoLibrary to retrieve the photoes from iPhone photoLibrary.

Any help would be appreaciated..

thanks in advance....


回答1:


Use ALAssetsLibrary for this:

 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);



回答2:


The code block below counts all videos and photos:

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


来源:https://stackoverflow.com/questions/11118196/how-to-get-total-count-of-photos-from-iphone-photolibrary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!