0

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

4

2 回答 2

2
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];

为什么在 cellForRowAtIndexPath 方法中获得 fullResolutionImage?并放入 selectedAllImages 数组.. 似乎 selectedAllImages 数组由 fullresolutionimages 无限填充(在滚动期间)。

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

为什么要创建 assetslibrary 并请求相同的资产(具有相同的 url)?

我认为你应该简化你的“cellForRowAtIndexPath”方法,在滚动过程中更轻量级。像这样的东西:

- (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;
}
于 2014-04-23T20:25:40.613 回答
0

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.

于 2014-04-23T19:14:23.293 回答