我想从相机胶卷访问所有照片我的代码工作正常如果我在相机胶卷中有少量图像然后应用程序工作正常但如果我有超过1000张照片然后应用程序崩溃在这里是我的代码
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
NSMutableArray *arrImages=[[NSMutableArray alloc]init];
NSString *albumName = [group valueForProperty:ALAssetsGroupPropertyName];
if (albumName !=nil) {
[arrGroupsNames addObject:albumName];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
UIImage *img=[self imageForAsset:asset];
[arrImages addObject:img];
}
}
];
[arrGroups addObject:arrImages];
}
[self.tableVieww reloadData];
}
failureBlock:^(NSError *error)
{
// User did not allow access to library
NSLog(@"error");
}
];
这是从ALAsset返回图像的函数
-(UIImage*) imageForAsset:(ALAsset*) aAsset{
ALAssetRepresentation *rep;
rep = [aAsset defaultRepresentation];
return [UIImage imageWithCGImage:[rep fullResolutionImage]];
}
答案 0 :(得分:2)
问题是,要为用户相机胶卷中的每张照片创建一个UIImage
,您将会耗尽内存。这可以很容易地生成1000多张图像,使用fullResolutionImage
将获得最大版本的图像。
要解决此问题,请将UIImage
放入数组中,而不是编写ALAsset
数组。然后,只有在屏幕上需要时,才能从UIImage
创建ALAsset
。
答案 1 :(得分:1)
您将所有1000 + UIImage
存储在一个数组中,并且内存不足。我有同样的问题,我的2000多张图片,并使用下面的实现为我工作。
我还建议您在显示图片时使用[asset thumbnail]
,特别是如果它仅用于UITableViewCell
或用于其他不需要图像完全分辨率的用途。
这是一个解决方案:
//First you need to declare `ALAssetsLibrary *al` as a global variable in the class
//
//like this:
//
//.h
@interface YOURCLASS : UIViewController
{
ALAssetsLibrary *al;
}
@end
//
//or inside your .m
//
@interface YOURCLASS ()
{
ALAssetsLibrary *al;
}
---
// you need to do this to check if 'al' is already declared and to declare once
if (al == nil)
al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
...
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
/*//your code
UIImage *img=[self imageForAsset:asset];
[arrImages addObject:img];
*/
//instead of storing the actual UIImage object just store the `asset` in your array
//like
[arrImages addObject:asset];
}
}];
...
}
failureBlock:^(NSError *error)
{
// User did not allow access to library
NSLog(@"error");
}];
在您的表格委托cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
//you need to get the image using the stored asset
//like:
//cast what's inside the array
ALAsset *asset = (ALAsset *)self.arrGroups[indexPath.row];
//self.arrGroups[indexPath.row];
//if just for example i dont know how you access your datas
cell.imageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
//getting the full resolution of the image
//[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]
//
//or you can simply use what you already have `[self imageForAsset:asset]`
//cell.imageView.image = [self imageForAsset:asset];
...
}
希望对你有所帮助,干杯!
答案 2 :(得分:0)
-(void)loadCameraPhotos{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
NSMutableArray *arrImages=[[NSMutableArray alloc]init];
NSString *albumName = [group valueForProperty:ALAssetsGroupPropertyName];
if (albumName !=nil) {
[arrGroupsNames addObject:albumName];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
// if (arrImages.count<7) {
//UIImage *img=[self imageForAsset:asset];
[arrImages addObject:asset];
//}else
// *stop=YES;
}
}
];
[arrGroups addObject:arrImages];
}
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.tableVieww reloadData];
}
failureBlock:^(NSError *error)
{
// User did not allow access to library
NSLog(@"error");
}
];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"PhotoSelectionTableViewCell";
PhotoSelectionTableViewCell* cell = (PhotoSelectionTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PhotoSelectionTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class] ]){
cell = (PhotoSelectionTableViewCell *)currentObject;
break;
}
}
}
NSArray *arrtemp=[arrGroups objectAtIndex:indexPath.section];
cell.lblTitle.text=[arrGroupsNames objectAtIndex:indexPath.section];
cell.lblCount.text=[NSString stringWithFormat:@"%lu",(unsigned long)arrtemp.count];
ALAsset *asset = (ALAsset *)[arrtemp lastObject];
cell.imgViewThumbnail.image=[UIImage imageWithCGImage:[asset thumbnail]];;
return cell;
}
答案 3 :(得分:0)
我真的怀疑这个解决方案。如果您想要加载太多照片,必须使用NSCache,因为您可以看到此存储库中的详细信息https://github.com/johnil/JFImagePickerController