我想将所有图像从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];
}

最佳答案

你能看下面的代码吗?希望对您有帮助。

从图库中获取所有图像

查看控制器头文件(.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/

10-11 22:17
查看更多