Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData : store images to DB or not?

Tags:

I am making an app that takes photos from web site for some Username and shows it in a UITable with username then when clicking user name it shows photos for this user and then clicking to name of photo it shows full screen photo.

My question is I am using NSData to get photos from internet. Do I have to save the data to CoreData? When pressing name of user it creates NSData and downloads photos from internet and shows them on UITable. And it takes time.

What is good approach? and How can save this images to CoreData?

I am using this method

NSData *imageData=[flickr dataForPhotoID:firstPhoto.id fromFarm:firstPhoto.farm  onServer:firstPhoto.server withSecret:firstPhoto.secret inFormat:  FlickrFetcherPhotoFormatSquare]; 

and here definition of dataForPhotoID method

- (NSData *)dataForPhotoID:(NSString *)photoID fromFarm:(NSString *)farm       onServer:(NSString *)server withSecret:(NSString *)secret   inFormat:(FlickrFetcherPhotoFormat)format {  #if TEST_HIGH_NETWORK_LATENCY sleep(1); #endif  NSString *formatString;  switch (format) {     case FlickrFetcherPhotoFormatSquare:    formatString = @"s"; break;     case FlickrFetcherPhotoFormatLarge:     formatString = @"b"; break; }  NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_%@.jpg", farm, server, photoID, secret, formatString]; NSURL *url = [NSURL URLWithString:photoURLString];  return [NSData dataWithContentsOfURL:url]; } 
like image 811
Ercan Avatar asked Apr 03 '10 23:04

Ercan


People also ask

Can we store image in Core Data?

Yes, we can store the images with the core data but not directly as UIImage. This can be achieved by converting UIImage to Data.

Should I use Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

How do I save an image in Core Data in Swift?

Creating the Model In that entity , create one attribute . Name it img and make sure the attribute type is Binary Data, then click on the img attribute and go to Data Model Inspector. Check the box Allows External Storage. By checking this box, Core Data saves a reference to the data which will make for faster access.


2 Answers

First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.

Second, the rule for storing binary data is:

  • < 100kb store in the same table as the relevant data
  • < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
  • 1mb store on disk and reference it inside of Core Data

Update

The storage inside of Core Data should be binary and you can write accessor methods for it. Take a look at this answer: Core data images from desktop to iphone

Update

The example code I linked to describes how to create accessors in your NSManagedObject subclass that will convert the image back and forth between a UIImage and binary data.

like image 95
Marcus S. Zarra Avatar answered Oct 25 '22 14:10

Marcus S. Zarra


You can simply store UIImage objects in CoreData directly, just use Transformable data type, and you are ready to go

like image 44
Khaled Khaldi Avatar answered Oct 25 '22 16:10

Khaled Khaldi