【问题标题】:how to get total count of photos from iphone photoLibrary.?如何从 iphone 照片库中获取照片总数。?
【发布时间】:2012-06-20 11:05:32
【问题描述】:

我想获取 photoLibrary 中的照片数量。目前,我能够从 photoLibrary 中获取照片并逐个添加到 myApp 的 Document 目录中。但我想要的是,一次将所有照片从 photoLibrary 保存到 myApp 的 Document 目录中。这就是为什么我需要 photoLibrary 中的照片计数。我使用 UIImagePickerControllerSourceTypePhotoLibrary 从 iPhone photoLibrary 中检索照片。

任何帮助将不胜感激..

提前谢谢....

【问题讨论】:

标签: iphone ios photolibrary


【解决方案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);

【讨论】:

    【解决方案2】:

    下面的代码块统计所有视频和照片:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多