首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从iphone photoLibrary中获取照片总数?

如何从iphone photoLibrary中获取照片总数?
EN

Stack Overflow用户
提问于 2012-06-20 19:05:32
回答 2查看 2.4K关注 0票数 0

我想要获取photoLibrary中的照片数量。目前,我可以从photoLibrary获取照片,并逐一添加到我的应用程序的文档目录中。但我想要的是,保存所有的照片从photoLibrary到文件目录的myApp所有的一次。这就是为什么我需要在photoLibrary中计算照片的数量。我已经使用UIImagePickerControllerSourceTypePhotoLibrary从iPhone photoLibrary检索了照片。

任何帮助都将受到欢迎..

提前感谢....

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-20 19:34:00

使用ALAssetsLibrary执行以下操作:

代码语言:javascript
复制
 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);
票数 1
EN

Stack Overflow用户

发布于 2014-03-17 15:13:08

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

代码语言:javascript
复制
__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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11118196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档