从设备获取所有图像并在uicollectionview中显示

时间:2015-07-07 10:19:31

标签: ios objective-c uicollectionview

我想在uicollectionview中显示从设备到我的应用的所有图像。 并希望从uicollectionview中选择多个图像。我看了很多节目。ELCImagePickerController

但我无法正确理解。 请帮我... 谢谢

这个链接工作得很好...... Multi-Select ImagePicker 但是如何从按钮Done中将选定的图像输入到数组中 selected images

当我按下数组中显示的完成按钮图像时......

<UIImage: 0x7fca78772510>, {485, 303}

所以,我如何在我的收藏视图中获得这个图像..帮助我们......

1 个答案:

答案 0 :(得分:2)

从图库中获取所有图片

查看控制器标题(.h)文件..

#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