Question

I am using UICollectionView to show all the images available in my library using assets, its displaying all the images but when I scroll up and down many times , I receive a memory issue in the device and after some time it gets crashed saying CRASHED DUE TO MEMORY PRESSURE code used are as follows

creates here the collectionview and settings its delegate

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
allPhotosCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(5, 5, 310, 481)collectionViewLayout:layout];
[allPhotosCollectionView setDelegate:self];
[allPhotosCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[allPhotosCollectionView setDataSource:self];
[self.view addSubview:allPhotosCollectionView];

delegate methods

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return [allImageArray count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:       (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath   *)indexPath
{
return CGSizeMake(95, 100);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"Cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// allimageArray has the Assest URL fo the images
NSURL *aURL=[[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@",[allImageArray objectAtIndex:indexPath.row]]];
[assestLibrary assetForURL:aURL resultBlock:^(ALAsset *asset)
 {
     UIImage  *copyOfOriginalImager = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
     UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
     NSData *imageData;
     imageData=UIImageJPEGRepresentation(copyOfOriginalImager, 0.4);
     [imageView setImage:[UIImage imageWithData:imageData]];
  [cell.contentView addSubview:imageView];
  }
        failureBlock:^(NSError *error)
  {
     // error handling
     NSLog(@"failure-----");
    }];
  return cell;
  }

I am facing many problems in iOS 7 , app crash due to memory pressure , please explain that too

Was it helpful?

Solution

its due to assest library. Once u fetched the image using assest libary just store it in some variable and re-use that image in

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

while scrolling the collection view it will fetch the image again and again using asset libary this will cause huge memory leak.

Edit:

 UIImage *img =[imageDictionary objectForKey:imageName];
                if(!img)
                {

                    NSURL *asseturl = [NSURL URLWithString:imageName];
                    img = [McAssetReader readImage:asseturl];
                    [imageDictionary setObject:img forKey:imageName];
                }

where imageDictionary is the Global Dictionay for holding the image. imageName is the url for particular image.

OTHER TIPS

There are two issues with your code:

  • You're keep adding imageViews to cell without even checking if one already exist, hence memory usage is growing rapidly;

  • You're not caching results of your image transformation and keep reading/transforming on every cell dequeue, hence you're keep pressure on disk IO and memory;

Cache and reuse technics will most likely fix your problems.

I can see issue here. Surely It will lead to memory error.

UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
.
.
.    
[cell.contentView addSubview:imageView];

See this line. You are creating imageView everytime and holding that image into imageview.

So you can do this instead of first line.

UIImageView *imageView = [cell.contentView viewWithTag:ImageViewTag];
if (!imageView)
{
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
    [cell.contentView addSubview:imageView];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top