从 iphone 图片库中获取所有图片并在 UICollectionView 中显示
Posted
技术标签:
【中文标题】从 iphone 图片库中获取所有图片并在 UICollectionView 中显示【英文标题】:Get all Images from iphone image library and show in UICollectionView 【发布时间】:2014-12-10 11:40:08 【问题描述】:我想将 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];
【问题讨论】:
***.com/questions/12633843/… 如果你有这么多图片,那么加载会占用大量内存。尝试使用更少的图片 我只有 2 张图片 @KiritModi 我已经检查了这个问题,但没有得到正确的答案 使用演示。 github.com/chiunam/CTAssetsPickerController 【参考方案1】:请看下面的代码。我希望它对你有帮助。
从图库中获取所有图片
查看控制器头(.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
【讨论】:
【参考方案2】:你忘了重新加载collectionView数据,在获取所有图片后插入这行代码:
[self.collectionView reloadData];
但是,我建议您使用 Photos Framework 而不是 ALAssetsLibrary,因为 ALAssetsLibrary 已被弃用。这里(link) 是照片框架获取所有视频的示例,您必须对其进行修改以获取照片而不是视频。
【讨论】:
以上是关于从 iphone 图片库中获取所有图片并在 UICollectionView 中显示的主要内容,如果未能解决你的问题,请参考以下文章