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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top