0

photoLibrary にある写真の枚数を取得したい。現在、私は photoLibrary から写真を取得し、myApp の Document ディレクトリに 1 つずつ追加することができます。しかし、私が望むのは、すべての写真を photoLibrary から myApp の Document ディレクトリに一度に保存することです。そのため、photoLibrary の写真の数が必要です。UIImagePickerControllerSourceTypePhotoLibrary を使用して、iPhone photoLibrary から写真を取得しました。

どんな助けでも感謝されます..

前もって感謝します....

4

2 に答える 2

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);
于 2012-06-20T11:34:00.397 に答える