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

Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20

2 Answers2

2
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;
}
Aleksey
  • 875
  • 8
  • 8
  • Thanks Aleksey.....your suggestion was really helpful and i accepted your answer.But one more doubt I have ie I need to save all UIImage in an NSMutableArray which i was doing like this--> image=[UIImage imageWithCGImage:[representation fullResolutionImage]]; [selectedAllImages addObject:image];.Can you tell me how to store all UIImage in an NSMutableArray – Nilesh Kumar Apr 24 '14 at 08:06
  • Because if I am using these two lines--> image=[UIImage imageWithCGImage:[representation fullResolutionImage]]; [selectedAllImages addObject:image];,I will get memory issues warning. – Nilesh Kumar Apr 24 '14 at 08:09
  • If you want to store all UIImage, put this cycle into some good place (viewDidLoad or something - not cellforrowatindexPath): for (ALAsset *asset in images) { UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]; [selectedAllImages addObject:image] } – Aleksey Apr 24 '14 at 09:11
  • But I'm not recommend doing it, because fullResolutionImage can be huge, and if you retain few fullResolutionImages - app can be killed due to memory pressure. – Aleksey Apr 24 '14 at 09:19
  • yep that's true.My requirement is that i want to save all UIImage in an NSMutableArray.How can i do that. – Nilesh Kumar Apr 24 '14 at 09:27
  • UIImage is like this "", "", "", "", "", "" – Nilesh Kumar Apr 24 '14 at 09:55
  • Better to use only thumbnails (or generate bigger thumbnails than ALAsset's thumbnail method provide), try also method 'fullScreenImage' instead of 'fullResolutionImage'. I belive you can avoid fullResolutionImage in your requirement. – Aleksey Apr 24 '14 at 09:58
  • ie to get UIImage from URL like assets-library – Nilesh Kumar Apr 24 '14 at 09:58
  • What you want to do with these UIImages - show to user, or process and save modified version? – Aleksey Apr 24 '14 at 09:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51335/discussion-between-wimcnilesh-and-aleksey) – Nilesh Kumar Apr 24 '14 at 10:11
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.

Abizern
  • 146,289
  • 39
  • 203
  • 257