DilumN
DilumN

Reputation: 2895

Received Memory Warning with UICollectionView to view Images

I have created a UICollectionView to view ALAssets horizontally. ALAssets are first stored to a MutableArray called assets. Then the collection View display those Assets by this method.

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    AssetCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

    ALAsset *asset = self.assets[indexPath.row];
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    UIImage *largeimage = [UIImage imageWithCGImage:iref];
    cell.imageView.image = largeimage;

    return cell;
}

But the assets collection is more than 100 images, the app gives a memory Warning for me. Memory usage also increased more than 50MB. How can get rid of this huge memory usage? What is the wrong thing I'm doing here?

Upvotes: 1

Views: 1116

Answers (2)

user3479453
user3479453

Reputation: 21

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. (Apple SDK guide) I guess it's because you try to load lots of large images at a time.

So, if you just want to show images in master collection view, you can use thumbnail images of ALAsset class instead.

When user touched one of images, show the 'fullResolutionImage' at that time.

Upvotes: 0

Daij-Djan
Daij-Djan

Reputation: 50099

you miss a release of the CGImage you create.

ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *rep = [asset defaultRepresentation]; 
CGImageRef iref = [rep fullResolutionImage]; //! creates
UIImage *largeimage = [UIImage imageWithCGImage:iref];
cell.imageView.image = largeimage;
CGImageRelease(iref); //! release

Upvotes: 3

Related Questions