0

I'm developing a messaging app (something like WhatsApp), users can send text and image messages to one another.

I created a Message object, subclass of NSManagedObject, to store a message details like:
sender_name, timestemp, message_text and message_image (for image messages)
* the average size of an image is like the size of an image taken from the iPhone camera roll library.

First questions is:
Is it a good idea to save the images to Core Data or will it be better to save them to the file system?

The problem is, when I'm in the chat view and I'm scrolling the tableView to see old messages, the scrolling performance get really bad when there are images to load from core data.

I'm using a NSFetchedResultsController to fetch the messages, and I'm fetching no more then 30 at once. I tried to improve the performance by creating a MessageImage object, also subclass of NSManagedObject, to store the UIImage.
A Message object has one-to-one relationship with MessageImage, I did this so when the fetcher loads 30 messages from Core Data, the UIImage itself won't be loaded until requested. But still I get bad performance...

So Second questions is:
What can I do to improve the scrolling performance ?

Eyal
  • 10,777
  • 18
  • 78
  • 130

2 Answers2

2

Create thumbnails of your images, that are the exact size they will be scaled to in the scroll view. This way you don't read all the high resolution images and don't have to scale them either. You can store the thumbnails with Core Data or the file system. I don't think there will be a huge difference. But definitely store the high resolution originals on the file system. When the user taps an image you can load them and show the original full screen.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • In which path is it common to save the images? Do u have any code example? – Eyal Jul 21 '12 at 22:20
  • In your app's documents folder. Or probably in a subfolder in it. You get the documents folder's path like this: `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];` – DrummerB Jul 22 '12 at 00:02
  • Any different between this and [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/fileName.jpg"]; – Eyal Jul 22 '12 at 07:30
1

I guess good practice is to not store images in core data that are large than - say - icon-size or so. Store them in the file system. Performance wise : you can use GCD to load images in the background, and then display them on the main thread as soon as they are loaded. Have a look here loading images from a background thread using blocks

Community
  • 1
  • 1
HeikoG
  • 1,793
  • 1
  • 20
  • 29