-1

I am trying to decide wether i should store my UIImages, which I get from my database and add to a data model inside the app, as UIImage() or as Data() or as URL().

What is the most effective method in terms of taking up the least storage space and being the fastest in displaying?

Use case: I have a similar use case to instagram basic functionality

Currently my DM looks as follows:

class Post: NSObject {

      var media = UIImage()
      var user = User()
      var snapshot : [String : AnyObject]?

}

helpful link: This link may be of use to those still seeking help: Link.

  • Have you looked here: https://stackoverflow.com/questions/9722603/storing-image-in-database-directly-or-as-base64-data –  Mar 03 '19 at 21:24
  • Yes, I have but I felt that my question is a bit more specific. –  Mar 03 '19 at 21:25
  • You typically don't store binary data in a database. You typically use a file storage system for that, such as Google Cloud Storage, via the Firebase SDKs. – Doug Stevenson Mar 03 '19 at 21:31
  • @DougStevenson yes I know and do: What I mean is when I fetch the data should i store it as say a URL or as a UIImage (That is, live in the code) –  Mar 03 '19 at 21:35
  • Which one suits your case better, from your own perspective? Since you haven't said anything about how you intend to use either the UIImage or the URL, it's really hard to say which once is better or more useful. For Android, most developers offload the handling of images to another library and never bother to hold the entire contents of an image in memory. But it may suit some cases. – Doug Stevenson Mar 03 '19 at 21:41
  • see update @DougStevenson –  Mar 03 '19 at 22:00
  • 2
    Definitely an absolute String representing the URL of the image. Unless you have static `posts` in your app, which you don't, there is really no reason to store an entire image in your `Post` object. There are several reasons for this but the two most important ones would be. 1) You are gonna be using tons of memory if you store image or data in each `Post` object. And 2) at some point you will find the need to make a caching system to display the images that have already been downloaded from firebase storage. Otherwise you will be downloading the images over and over again. – Galo Torres Sevilla Mar 03 '19 at 22:20
  • 2
    The cache is gonna need some sort of identifier to return the correct image so what better than the URL for this. I'll say that in 99% of the cases it will be better to use the URL and not the image itself. Let the images be downloaded as needed. – Galo Torres Sevilla Mar 03 '19 at 22:21
  • Ok great! So it sounds like a storing a string representation into each post object and just converting that to UIImage when needed is the best option for this use case. If you provide this as an answer I will accept. :) –  Mar 03 '19 at 22:23
  • Glad I could help :) – Galo Torres Sevilla Mar 03 '19 at 22:32

1 Answers1

0

Definitely an absolute String representing the URL of the image. Unless you have static posts in your app, which you don't, there is really no reason to store an entire image in your Post object. There are several reasons for this but the two most important ones would be.

1) You are gonna be using tons of memory if you store image or data in each Post object.

2) At some point you will find the need to make a caching system to display the images that have already been downloaded from firebase storage. Otherwise you will be downloading the images over and over again.

The cache is gonna need some sort of identifier to return the correct image so what better than the URL for this. I'll say that in 99% of the cases it will be better to use the URL and not the image itself. Let the images be downloaded as needed.

Galo Torres Sevilla
  • 1,306
  • 2
  • 6
  • 11
  • Hey so I am coming back because I implemented such a solution however when I did I found that as I taped from one tableViewCell to the next it was slow to react, I belive this was due to a conversion function wich I had to make to convert teh url to a image. –  Mar 04 '19 at 02:49
  • 1
    Did you implement a cache already? Images in tableViews should always be cached, otherwise yes, it will be quite slow. There are several ways of implementing caches. This guy for example shows you how to implement a simple one https://www.youtube.com/watch?v=ilwEMj7mWCY or you can use one that is already available. SDWebImage is one of the most famous ones, but for me in a couple of cases it was a bit slow. I used kingFisher: https://github.com/onevcat/Kingfisher in a couple of projects and I really liked it. – Galo Torres Sevilla Mar 04 '19 at 12:07