我想在photoLibrary中获取照片数量。目前,我能够从photoLibrary中获取照片,并一个一个地添加到myApp的文档目录中。但是我想要的是,一次将所有照片从photoLibrary保存到myApp的Document Directry中。那就是为什么我需要在photoLibrary中计算photoes的原因。我已经使用UIImagePickerControllerSourceTypePhotoLibrary从iPhone photoLibrary检索照片。
任何帮助将不胜感激..
提前致谢....
最佳答案
为此,请使用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);