19

photoLibrary からすべての写真を取得したい。直接使用できる方法または例を希望します。

4

3 に答える 3

29

//View Controller header(.h) file..

#import <UIKit/UIKit.h>
#include <AssetsLibrary/AssetsLibrary.h> 

@interface getPhotoLibViewController : UIViewController
{
 ALAssetsLibrary *library;
 NSArray *imageArray;
 NSMutableArray *mutableArray;
}

-(void)allPhotosCollected:(NSArray*)imgArray;

 @end

//実装ファイル

グローバルカウント変数を次のように宣言します

static int count=0;

@implementation getPhotoLibViewController

-(void)getAllPictures
{
 imageArray=[[NSArray alloc] init];
 mutableArray =[[NSMutableArray alloc]init];
 NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

 library = [[ALAssetsLibrary alloc] init];

 void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
  if(result != nil) {
   if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
    [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

    NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

    [library assetForURL:url
             resultBlock:^(ALAsset *asset) {
              [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

              if ([mutableArray count]==count)
              {
               imageArray=[[NSArray alloc] initWithArray:mutableArray];
               [self allPhotosCollected:imageArray];
              }
             }
            failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

   } 
  }
 };

 NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

 void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
  if(group != nil) {
   [group enumerateAssetsUsingBlock:assetEnumerator];
   [assetGroups addObject:group];
   count=[group numberOfAssets];
  }
 };

 assetGroups = [[NSMutableArray alloc] init];

 [library enumerateGroupsWithTypes:ALAssetsGroupAll
                        usingBlock:assetGroupEnumerator
                      failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}

-(void)allPhotosCollected:(NSArray*)imgArray
{
 //write your code here after getting all the photos from library...
 NSLog(@"all pictures are %@",imgArray);
}

@end

フォト ライブラリから写真を取得するには、getAllPicture メソッドを使用します。

または、このブログを見ることができますhttp://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

于 2012-09-28T05:37:20.983 に答える
7

ALAssetsLibrary現在は廃止されており、新しいPhoto Frameworkものです。Objective Cカメラロールからすべての写真を取得して保存しNSArray、コレクションビューに表示する独自の関数を作成しました

 NSArray *imageArray;
 NSMutableArray *mutableArray;

-(void)getAllPhotosFromCamera
{
    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];

    PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
    requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    requestOptions.synchronous = true;
    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

    NSLog(@"%d",(int)result.count);

    PHImageManager *manager = [PHImageManager defaultManager];
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];

    // assets contains PHAsset objects.

    __block UIImage *ima;
    for (PHAsset *asset in result) {
        // Do something with the asset

        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize
                          contentMode:PHImageContentModeDefault
                              options:requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                            ima = image;

                            [images addObject:ima];
                        }];


    }

    imageArray = [images copy];  // You can direct use NSMutuable Array images
}
于 2016-03-02T09:08:49.850 に答える