0

I have a ViewController where I want a UITableView to contain 5-30 user profiles (name, profile pic, about me etc) it is one of the tabs in a TabController. The profile content change rarely, but once a week, some profiles are added/removed.

The profile content comes in the form of a json object from our server that I parse into a [[String:Anyobject]] array.
One of the fields contain the URL to the profile picture (only like 30kb each).

My question is: What is the best pattern for getting to the pictures and displaying them in the TV?

First, I am guessing it is preferable to do one single URL request, instead of 30 individual. How can I accomplish that? example URL to a profile image is:

www.example.com/assets/profileImages/useriD.png.

Second, should I save the pictures to CoreData first and then fetch them from there (if so how?) or should I just do a URL request inside the cell like I do to set the profile name etc ?

If anyone could point me in the right direction about the correct pattern for this that would be awesome, thank you !

KML
  • 2,302
  • 5
  • 26
  • 47

2 Answers2

2

Unless your server has a scheme to do so, you can't get multiple images in a single request. You'll need to make one request per image (though iOS and the server should cooperate to maintain a single HTTP connection).

How you do caching is up to you, but:

  • you can simply rely on iOS caching (provided the files have appropriate Cache-Control/Expires headers), possibly being a bit more insistent on cache use by setting the appropriate cache policy in your NSURLRequest (e.g. ReturnCacheDataElseLoad)

  • you can add just a bit of local caching inside your app of the decoded images (UIImage). A simple NSDictionary inside your view controller should be enough

  • you can store the images (encoded data) on disk. If you do so, instead of reinventing the wheel, you may want to use libraries such as SDWebImage. The docs of SDWebImage include numerous examples, including an UITableView example.

  • you can store the images (encoded data) in Core Data, though their size does not make them ideal candidates for this.

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Thank you, this was a very good answer, SDWebImage works unbelievably well. – KML Dec 15 '15 at 15:45
  • Similar to SDWebImage, there is this swift library [https://github.com/Haneke/HanekeSwift] witch has same functionalities. – Lucho Dec 15 '15 at 16:24
  • SDWebImage is outdated at this point (still uses deprecated NSURLConnection etc). You might want to try some alternative [libraries](http://stackoverflow.com/questions/16663618/async-image-loading-from-url-inside-a-uitableview-cell-image-changes-to-wrong/32601838#32601838) – kean Dec 15 '15 at 18:12
1

+1 for SDWebImage, that jcaron said.
Its a really powerful tool. You can just simple add the url link in cellForRowAtIndexPath for each UIImage, the rest is handled by the SDWebImage.
This lib downloads the images asynchronously, the performance is insane, and you can set the storing interval too.

Peter
  • 111
  • 8