如何从iPhone photoLibrary获取照片总数? [英] how to get total count of photos from iphone photoLibrary.?

查看:142
本文介绍了如何从iPhone photoLibrary获取照片总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取photoLibrary中的照片数量.目前,我可以从photoLibrary中获取照片,并一个一个地添加到myApp的文档目录中.但是我想要的是,一次将所有照片从photoLibrary保存到myApp的Document Directry中.那就是为什么我需要在photoLibrary中计算photoes的原因.我已经使用UIImagePickerControllerSourceTypePhotoLibrary从iPhone photoLibrary检索照片.

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.

将提供任何帮助.

提前谢谢....

推荐答案

为此使用ALAssetsLibrary:

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

这篇关于如何从iPhone photoLibrary获取照片总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆