Search code examples
ioscore-dataicarousel

Memory trouble with iCarousel and images from coredata


I have a basic implementation for iCarousel where I am pulling images from a core-data store. I am experiencing memory related crashes when you get a lot of images loaded, as I guess is expected. The problem is I don't know of any way to use the AsyncImageView library since there is no NSURL reverence for a core-data object. Anyone know another way to manage this memory issue?

Here is my iCarousel viewForItemAtIndex

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

    JournalEntry *journalEntry = (JournalEntry *)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];

    if (view == nil)
    {
        view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240.0f, 240.0f)] autorelease];
        view.contentMode = UIViewContentModeScaleAspectFit;
    }

    [((UIImageView *)view) setImage:[journalEntry.image valueForKey:@"image"]];

    NSLog(@"%i", index);

    return view;
}

For reference here is code for adding images to my core-data store straight out of the iPhoneCoreDataRecipes sample code.

@implementation ImageToDataTransformer


+ (BOOL)allowsReverseTransformation {
    return YES;
}

+ (Class)transformedValueClass {
    return [NSData class];
}


- (id)transformedValue:(id)value {
    NSData *data = UIImagePNGRepresentation(value);
    return data;
}


- (id)reverseTransformedValue:(id)value {
    UIImage *uiImage = [[UIImage alloc] initWithData:value];
    return [uiImage autorelease];
}

@end

Solution

  • After studying this for a while it turns out the large memory growth is due to the coredata cache and not iCarousel. This can be cleared by calling reset on the managedObjectContext.