2

Possible Duplicate:
How should I store UIImages within my Core Data database?

I decided to save my images in Core Data as opposed to saving them on the disk. I looked at Apple's sample code called Locations. However, that code target iOS 4, I seen another Stack Overflow post that this changed in iOS 5.

I've read about the allows external storage, and i'm saving the image as binary data in my core data model.

The below code retrieves an image from core data, I found this online. However, I can not find any tutorials on how to save the image to core data in binary format, or any tutorials on working with core data and iOS 5 with images.

NSData *savedBinaryData = entity.smallImage;
self.smallImage.image = [UIImage imageWithData:savedBinaryData];
Community
  • 1
  • 1
Vikings
  • 2,527
  • 32
  • 45
  • See creativ's answer. Personally, I have seen horrible performance when using the database to store images. I've subsequently gone back to storing the images in the Documents folder and keeping track of filenames in the database. It seems a lot less elegant, but, for me at least, it was much, much faster. If you're doing this with lots of images, you might want to benchmark both approaches, but I think you'll find that the Documents folder is much quicker. – Rob Jul 15 '12 at 20:22
  • @RobertRyan i've had some many issues with this i'm going crazy. I used GCD, but that still did not get me the results I wanted. My plan is to store the thumbnails in Core data 60 X 60, and the full image in the documents, then load that when necessary – Vikings Jul 15 '12 at 20:24
  • Certainly do what you want, but I personally store both images and thumbnails in `Documents`, just in different folders. Not sure if I see the benefit of storing some images in `Documents` and others in Core Data. In terms of your comment that you "still did not get ... the results I wanted", I'm not sure what you got and why it was unsatisfactory, so I cannot comment on that. – Rob Jul 15 '12 at 20:30
  • @RobertRyan do you have any apps that are in the app store that use the documents method, i'd like to see the performance that you got out of this method – Vikings Jul 15 '12 at 20:34
  • Vikings, you really should give creative the "check" for providing the proper answer to your question. – David H Jul 15 '12 at 20:36
  • 2
    I have an app in the store that is working quite well with thumbnails in the database and larger images stored in flash. I save the thumbnails as jpegs (originals are also jpeg) so size is smaller but it will take a bit longer to rasterize them most likely. – David H Jul 15 '12 at 20:39
  • @Vikings I have an app on the store that uses Documents for all images and it's plenty fast, but I'd encourage you to just experiment yourself. Write a loop that loads images many times from your database, and do it again `imageWithContentsOfFile`. Compare and contrast your own results. But I would bet that the performance difference may be indistinguishable for a few thumbnail-sized images. – Rob Jul 15 '12 at 20:55
  • @DavidH good to someone is experienced good results with that method – Vikings Jul 15 '12 at 22:21
  • @BradLarson A packer's fan would close my post. I saw the other post. However, they were talking about iOS 4 and storing images, not much said about how to do this with iOS 5. – Vikings Jul 15 '12 at 22:23
  • @Vikings - Sorry, I wanted to direct to that one and a comment might not have been visible here. Core Data has not changed dramatically enough in iOS 5.0 or Lion to invalidate the approach there, which is one I had suggested to me by the Core Data engineers. In fact, none of the fundamentals of Core Data have been made obsolete with iOS 5.0 or 6.0, they've just added a few things to make threading and other actions a little easier. A transformable attribute will do what creativ describes in their answer, only it will cleanly abstract that away from you. I use this in an iOS 5.0 app today. – Brad Larson Jul 15 '12 at 22:32
  • FYI, I just ran some diagnostics in iOS 5.1, doing 10,000 retrievals of a 152px x 152px jpg image from Documents via `imageWithContentsOfFile` vs from Core Data, and retrieving it from Documents was 40% faster. Clearly if you use some caching mechanism (such as `imageNamed`) and occasionally retrieve the same image, there is an even more dramatic improvement. – Rob Jul 16 '12 at 00:40
  • @BradLarson I was joking about the packer's fan commet, I did read the other post, thank you! – Vikings Jul 16 '12 at 01:20
  • @RobertRyan that is a big difference, I tried the GCD method, but was getting slow results, and table was glitchy. Check out my other post I edited in the code I used if you want to take a look. http://stackoverflow.com/questions/11486828/table-view-scrolling-async – Vikings Jul 16 '12 at 01:21

1 Answers1

6

You can get the binary data from a UIImage by using UIImagePNGRepresentation(UIImage* image) and then just save the NSData in core data.

So you could just do something like:

[object setSmallImage: UIImagePNGRepresentation(image)];
creativ
  • 314
  • 3
  • 10