Chris Campbell
Chris Campbell

Reputation: 182

RestKit CoreData and UIImage

I'm using Rest Kit with Core Data, one of the Core Data entities has an attribute 'image' that has a binary type.

I'm still in mockup stage so the image is populated with this code:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lorempixel.com/60/60/people"]]];
entry.image = UIImagePNGRepresentation(image);

Another tab has a collection view that uses fetchedResultsController.

After creating a new entity, if I only save the context the image works fine.

But if I push the entity to the web server using 'postObject:' the image is corrupted when it comes back from the server. I've confirmed the server receives the same string representation of the image "<2f396a2f 34414151 536b5a4a 52674142 ... 6a6e502f 32513d3d>" and stores it directly into a MySQL column of type long blob and at all points the string representation is the same.

But when the collection view is populated using a server call via RestKit the entities image is invalid. I'm think the issue is the data is being converted into the data representation of the description of the data.

Does anyone have a working example with images. The only thing I can think of is that I need to add a custom transformation, but the documentation and examples are lacking as far as how to actually implement one.

Upvotes: 1

Views: 241

Answers (1)

Wain
Wain

Reputation: 119031

RestKit is storing the plain NSData for the image in Core Data - it has no idea what else you might want to do with it. Generally you don't want to manage images directly in Core Data or using RestKit.

Generally, store the path of the image in Core Data and the file on disk. Download them asynchronously (from the URL's which would also be in Core Data).

For uploading, you could make RestKit upload the data, but you probably actually want to file upload or convert to base64. You will need to write some code for this (which you could have RestKit pick up by using the key of the method name that returns the appropriate data). A similar process will work for mapping the data in.

RestKit data transformers are hard to make work in this situation as you are converting between data and strings and they are too general to be able to intercept accurately.

Upvotes: 1

Related Questions