Search code examples
iosobjective-cuicollectionviewalassetslibrary

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?


Solution

  • 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