Terminated due to Memory Pressure By using AssetsLibrary.framework

559 views Asked by At

I am using AssetsLibrary.framework for fetching images from device gallery.I successfully fetched all images from gallery and displayed on my table view.Problem comes when I scroll up and down many times , I receive a memory issue warning getting printed on console and after some time it gets crashed saying CRASHED DUE TO MEMORY PRESSURE.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    TableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    ALAsset *asset = [self.images objectAtIndex:indexPath.row];
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
    [selectedAllImages addObject:image];
    url = [representation url];

    NSURL *imageURL=url;
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        fileName = [representation filename];
        cell.cellLabel.text=fileName;
    };
    assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageURL
                   resultBlock:resultblock
                  failureBlock:nil];
    [cell.cellImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
    return cell;
}

So I need help where the mistake is?Thanks

2

There are 2 answers

10
Aleksey On BEST ANSWER
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];

Why do you getting fullResolutionImage in cellForRowAtIndexPath method? And putting into selectedAllImages array.. it seems like selectedAllImages array fills by fullresolutionimages infinity (during scrolling).

assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
               resultBlock:resultblock
              failureBlock:nil];

Why you create assetslibrary and request for same asset (which have same url)?

I think you should simplify your 'cellForRowAtIndexPath' method, to be more lightweight during scrolling. Something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    TableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    ALAsset *asset = [self.images objectAtIndex:indexPath.row];
    ALAssetRepresentation *representation = [asset defaultRepresentation];

    cell.cellLabel.text = [representation filename];
    cell.cellImageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
    return cell;
}
0
Abizern On

The images you're loading from the asset library are going to be large. If your trying to load many of them on a table view then you will quickly run out of memory. The usual practice is to create scaled images from the larger images, which use a lot less memory.