我使用以下代码从照片库中获取图像,在显示它之前调整它的大小是接收内存警告。 它也因内存压力而终止。
-(void)readImages:(int)getAlbumImages
{
imagesArray = [[NSMutableArray alloc]init];
allImagesArray = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[allImagesArray addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[imagesArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
[picsTbl reloadData];
[loadingView setHidden:YES];
}
failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ];
}
}
else if(result == NULL){
[loadingView setHidden:YES];
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
NSLog(@"Number of assets in group :%ld",(long)[group numberOfAssets]);
NSLog(@"asset group is:%@",assetGroups);
}
NSLog(@"[group numberOfAssets] %d",[group numberOfAssets]);
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];
}
请帮忙。
答案 0 :(得分:1)
很明显,您无法在阵列中以全屏图像打开并存储所有照片,因为这会导致内存压力并最终导致内存故障,这导致了崩溃。
此外,声明
[picsTbl reloadData];
可能有问题,因为它不清楚执行哪个执行上下文,因为它是系统方法的完成处理程序。在这里,如果 picsTbl 是UITableView
,则必须是主线程。你应该确定实际情况。
您的问题需要采用不同的方法。具体来说,由于您可能正在处理大型数据(给定受限设备),因此您必须确保只处理一个映像,并且一次只将一个映像保留在内存中。 / p>
您可以获得一些如何顺序调用异步方法的想法 - 即,有效地序列化异步方法,在这里:
Force async tasks to run in sequence
ios programming: Using threads to add multiple images to library
答案 1 :(得分:0)
非ARC
block = ^(ALAsset *myasset)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// rest of block here
[pool release]; }
对于ARC
block = ^(ALAsset *myasset) { @autoreleasepool{ // rest of block here } }
以下是示例代码
{
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) {
@autoreleasepool {
UIImage *imageToSave = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
CGSize sizeToSave = CGSizeMake(30,30);
UIGraphicsBeginImageContextWithOptions(sizeToSave, NO, 0.f);
[imageToSave drawInRect:CGRectMake(0.f, 0.f, sizeToSave.width, sizeToSave.height)];
UIImage *finalImageToSave = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"mutableArray.count:%d",mutableArray.count);
if (finalImageToSave!=nil) {
[mutableArray addObject:finalImageToSave];
}
if ([mutableArray count]==89)//count
{
[imageArray addObjectsFromArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
}
failureBlock:^(NSError *error){
[[[UIAlertView alloc]initWithTitle:@"" message:@"operation was not successfull!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
NSLog(@"operation was not successfull!");
} ];
}
}else{
//handle UI
}