2

Using images with Core Data isn't a problem. There are a lot of examples out there on how to do that like: How should I store UIImages within my Core Data database?

I want to know how to use RestKit to download images and map them correctly to Core Data. There is a RestKit example on how to upload an image but not downloading and retrieving.

Right now my entities just have an attribute with the url to the image but I want to be able to access the images offline. I was thinking of doing some simple mapping like download an image and rename it to the id of the object it belongs to but before I recreate this wheel I was wondering if someone else knows the most 'correct' way to achieve this.

Community
  • 1
  • 1
Steve Moser
  • 7,647
  • 5
  • 55
  • 94

2 Answers2

1

I use JsonKit and ASIHTTPRequest, but the same principle applies - I use base64 encoding to store the image data as a string. It's a platform and language agnostic approach that fits well with the JSON standard.

Cocoa with Love's Matt Gallagher wrote a very clean category on NSData for base64 encoding and decoding here.

Jacob Jennings
  • 2,796
  • 1
  • 24
  • 26
1

I ended up just requesting the object with the image url and after receiving the object I grab the image for the url set it to the my object's image property. Simple

NSURL *imageURL; UIImage *selectedImage;

    for (Employee *employee in _employees){

        imageURL = [NSURL URLWithString:employee.imageURL];
        selectedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
        // Delete any existing image.
        NSManagedObject *oldImage = employee.image;
        if (oldImage != nil) {
            [employee.managedObjectContext deleteObject:oldImage];
        }

        // Create an image object for the new image.
        NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:employee.managedObjectContext];
        employee.image = image;

        // Set the image for the image managed object.
        [image setValue:selectedImage forKey:@"image"];

        // Create a thumbnail version of the image for the recipe object.
        CGSize size = selectedImage.size;
        CGFloat ratio = 0;
        if (size.width > size.height) {
            ratio = 100.0 / size.width;
        } else {
            ratio = 100.0 / size.height;
        }
        CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

        UIGraphicsBeginImageContext(rect.size);
        [selectedImage drawInRect:rect];
        employee.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
  • It looks like you are now requesting the image synchronously and without using Restkit. Right? Is there a way to request the image using RestKit as well? That would help me because of authentication being handled by restkit. – jpalten Jul 22 '12 at 06:48
  • That is correct. I don't know of a way to request a binary resource using RestKit but I would check this out: https://groups.google.com/forum/?fromgroups#!topic/restkit/q6wP_hQPEfM – Steve Moser Jul 23 '12 at 01:40
  • I made subclasses for RKRequest and RKResponse that will request a file (or image) and write the response to disk instead of memory. Works like a charm. – jpalten Jul 24 '12 at 20:53