Search code examples
alassetslibraryios7.1alassetsgroup

Terminated due to Memory Pressure By using AssetsLibrary.framework


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


Solution

  • 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;
    }