I have used following code to fetch images from photo library, resizing it before displaying it is receiving memory warnings. Also it gets terminated due to memory pressure.
-(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");}];
}
Please help.
It should be quite obvious that you can't open and store all photos as fullscreen images in an array since this will cause memory pressure and finally a memory failure, which leads to a crash.
Additionally, the statement
might be problematic since it is not clear on which execution context it is executed since it is a completion handler of a system method. Here, if picsTbl is a
UITableView
is must be the main thread. You should make sure it actually is the case.You need a different approach for your problem. Specifically, since you are potentially working with large data (given a restricted device) you must ensure you only process one image and only keep one image in memory at a time.
You can get some ideas how to sequentially invoke asynchronous methods - that is, effectively serializing asynchronous methods, here:
Force async tasks to run in sequence
ios programming: Using threads to add multiple images to library