ios - 从iphone图片库中获取所有图片并显示在UICollectionView中

标签 ios objective-c uicollectionview assetslibrary

我想将 iphone 图像库中的所有图像显示到 UICollectionView 但在完成 getAllPictures 方法后,allPhotosCollected:方法未调用且图像未显示到 UICollectionView

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getAllPictures];
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [imageArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];

    recipeImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];

    return cell;
}


-(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]]];



                                 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
{
    NSLog(@"all pictures are %@",imgArray);
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
}

最佳答案

请看下面的代码。希望对您有所帮助。

从图库中获取所有图像

查看 Controller 头文件(.h)文件..

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

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

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

@end

执行文件(.m)

将全局计数变量声明为

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

@结束 使用 getAllPicture 方法从照片库中获取照片。

或者你可以看看这个博客http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

关于ios - 从iphone图片库中获取所有图片并显示在UICollectionView中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26381804/

相关文章:

ios - 结构检查类型| swift

ios - Xcode 8.3中损坏的 Playground

ios - UICollectionView 中未声明的标识符错误

ios - 无法在 uicollectionview 中显示 uimage 集

asp.net - AES128 位加密字符串与 .net 上的不相似

ios - 拖动注释时的回调

ios - 完成按钮开始启用并在开始输入用户名后自行禁用,跳过验证方法

objective-c - 仍然对 Objective-C 的动态绑定(bind)感到困惑

objective-c - 所有模块列表

ios - 选择时 UICollectionviewCell backgroundColor 不更改