0

我想显示来自 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];
}
4

2 回答 2

2

你能不能看看下面的代码。我希望它对你有帮助。

从图库中获取所有图像

查看控制器头(.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);
 }

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

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

于 2015-12-29T11:07:20.020 回答
0

您忘记重新加载 collectionView 数据,在获取所有图像后插入这行代码:

[self.collectionView reloadData];

但是,我建议您使用 Photos Framework 而不是 ALAssetsLibrary,因为 ALAssetsLibrary 已被弃用。这里(链接)是照片框架获取所有视频的示例,您必须对其进行修改以获取照片而不是视频。

于 2016-08-16T13:02:04.713 回答