J.Doe
J.Doe

Reputation: 397

How to handle images that are loading in? Swift

I've currently got an app that has a primary focus of multiple pages populated by user submitted pictures in collection views.

I am saving images to a cache so it's not an issue after the initial load, but the first time the page shows, I've got a few issues:

1) It would look significantly better to have the images only show when they've ALL downloaded instead of them showing up randomly until they're all loaded in. I'm downloading my pictures in from Firebase Storage. Is there a way to do that?

2) I have labels that are representative of firebase data on my cells, but it obviously looks silly to have those numbers/labels showing before the picture is loaded in. How would this be handled?

3) When the current user submits an image into a cell, it looks silly to have the image waiting to be sent to firebase storage and then back, so I had it just instantly set that cell's image to the selected picture, but then when the image loads the image dissapears and then loads up the picture.

I assume this is because in my image cache I need to have :

func loadImageUsingNSCache(urlString: String){

  self.image = nil

at the beginning. So it's setting it to nil at the beginning of the load, and THEN sending in the picture.

I tried putting the self.image = nil just into my cellForRow, but I was still having massive issues with pictures flashing/being in the wrong spot until I put it here.

Any guidance on these would be greatly appreciated. I don't know if these need to be separate questions, but they seem tightly related enough to be grouped up.

Upvotes: 1

Views: 711

Answers (1)

Bienemann
Bienemann

Reputation: 152

For 1 and 2 you could create a dispatch group, add all downloads to that group and get notified when they finish. That way you could implement something of a loading screen and then present all the data at the same time.

As for 3, I don't know how your cache is implemented, but I'm pretty sure any decent caching engine would treat the uploaded and the downloaded images as different ones (even though it's technically the same picture).
A kind of dirty way of making it work would be to put the picture there when uploading like you are already doing, and then downloading it in the background and refreshing the image without animating. The user won't notice and your cache will be updated.

Upvotes: 1

Related Questions