9

I have multiple collection views on screen at one time that scroll horizontally. They are all filled with images. All of these images are being loaded in the background through the Parse api. I am running Instrument's allocations and the anonymous VM: ImageIO_JPEG_DATA category is taking up a majority of the memory being used. All memory in the app equals to about 40 and then this category is over 55, which puts the total right around 100. That category never goes down at all and just stays consistent. What can I do to free up this memory from the images in my collection views?

Here is the code for my collection view:

.m for my collection view controller

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

    if (cell) {
        [cell.loadingImageIndicator setHidden:NO];
        [cell.loadingImageIndicator startAnimating];

        id photo = [self.collectionViewPhotos objectAtIndex:indexPath.item];

        if ([photo isKindOfClass:[PFObject class]]) {
            PFObject *photoObject = (PFObject *)photo;

            PFFile *imageFile = [photoObject objectForKey:kPhotoPictureKey];
            [cell.cellImage setFile:imageFile];

            [cell.cellImage loadInBackground:^(UIImage *image, NSError *error) {
                [cell.cellImage setContentMode:UIViewContentModeScaleAspectFill];
                [cell.loadingImageIndicator stopAnimating];
                [cell.loadingImageIndicator setHidden:YES];
            }];
        } else if ([photo isKindOfClass:[UIImage class]]) {
            [cell.cellImage setImage:photo];
            [cell.cellImage setContentMode:UIViewContentModeScaleAspectFill];
        }
    }

    return cell;
}

.m for CollectionViewCell

- (void)prepareForReuse
{
    self.cellImage.image = nil;
}

- (void)dealloc
{
    self.cellImage = nil;
}

Edit: Photo of instruments enter image description here

BlueBear
  • 7,469
  • 6
  • 32
  • 47
  • Did you ever resolve this? I'm having a very similar issue. http://stackoverflow.com/questions/25633253/why-memory-warnings-with-4-mb-utilization-and-320-mb-free – davecom Sep 04 '14 at 07:06

1 Answers1

4

I had the same issue in a photo gallery-type app, and ran into the same problem with allocations in the so-called ImageIO_JPEG_DATA category accumulating and remaining "live" forever, causing my app to run out of memory. Oddly, this happenned only on the iPad I was testing on and NOT on the ios simulator (which showed no memory problems).

Brian's suggestion (below) worked for me. My app was originally using an array, each element of which contained - amongst other things - a UIImage. The images were used in various UIScrollViewControllers.

When I needed to load an image, if I used

[UIImage imageWithContentsOfFile:path]

rather than a direct reference to the UIImage in my array, the memory problem (caused by some inexplicable caching that ImageIO_Malloc was doing) was gone and the ImageIO_JPEG_DATA allocations stopped piling up and got released.

I would never have come up with this solution in a gazillion years on my own, so thanks to Brian.

Eric
  • 41
  • 2
  • In my app the Parse photo objects are stored into an array. Then the way to set the image is by getting the file for that object and load it into the imageView in the background. So there is no point where I am calling [UIImage imageNamed:]. So where could I implement this into my application to get this improvement? – BlueBear May 12 '14 at 03:33
  • Sorry @Jonathon I am not familiar with the Parse API. That said, the problem in MY app was created when I did what it looks like this line is doing: '[cell.cellImage setImage:photo];'- namely I took a UIImage and assigned (set) it to another UIImage variable. ie, I did something like 'imageToLoadInScrollView = [[myArrayOfImages objectAtIndex:index] thePicture];' To solve the problem I generated the path to the file of the image I needed and then replaced the line above with 'imageToLoadInScrollView = [UIImage imageWithContentsOfFile:pathToFileOfThePicture];' – Eric May 12 '14 at 11:26