Hi, Alex here from rebeloper.com and welcome back to the channel and welcome
back to the last episode of this series hopefully you enjoyed it. In this series
you learned how you can take, save your images into your devices library and
into a custom folder indeed and in the previous episode you have learned how
you can upload your UI images into firebase storage and that get that
download URL and set it into fire store. Now that was important because now we
are going to download that image with the help of that download URL. So without
further ado let's jump right into it.
Okay, now if you haven't already done so go ahead and take a look at those videos
so you better understand the structure of this app. But today we are going to
finally download our image from firebases
fire store, okay. Not fire store it involves fire store but the actual image
is in storage. So let's scroll all the way down to download photo and what we
first want to do here is basically start the activity indicator. So activity
indicator dot start animating. Now what do we actually want to do here. We want
to basically download the image and display it in our UI image view. Now for
that we want to know, we need to know the UID of our let's say only data
and luckily we have said that inside our user defaults like so. So first of all
let's get that but we are going to need a guard let statements. So guard let and
let's see here UID equals user defaults standard and let's see here
value for key and the key is my keys dot. I believe it was UID if I'm
correct, let me just double check that is correct. Okay, and let's set in here
else and I will just simply copy and paste one of these errors. So to simply
present alert error something went wrong. Okay, and that you take care of it
we have a return. So now we do have our UID. Now what do we want to do with this UID?
Well want to query our firestore database for documents that do have this UID.
So we want to create a query first. So let query equals and that is firestore
dot fire store and the collection will be our let's see what did I call
that my keys I believe it is images collection. There we go and we want to
grab where, so let's type it here where field is equal to and let me just type
that in and then we will better understand. But the field is my keys dot
UID and the equal to is our UID that we have just guard let unwrapped.
Let me add in here some new lines so is is better readable. So now let's call
upon our query get documents and we will use this one with a simple completion
and the let's add that in there and now we get back an optional snapshot and an
optional error. So let's add that snap shot in there and our error. Of course we
take care of the error and I will just copy and paste all of this code from
here I don't want to mess up time here so waste time for adding that and now
that we have our snapshot we want to guard let unwrap it. So let's do that.
Guard let snapshot equals snapshot else. Else what do we want to do? Well just
present an alert with something went wrong, okay. Now what do we want to do
with this snapshot? Well there is some, there are some documents in the snapshot
and those documents have some data. Basically we want to grab
our data. Now we want to grab our first document. So let's do that.
Let document equals or rather yet let's grab the whole data here. Data equals
snapshot dot documents dot first because this is where we have our optional and
data. There we go. Okay, now that we do have our data we
want to grab our let's call it key pair value we want
to grab basically in the image URL. So let URL equals data and we will
grab our my keys dot image URL, okay. And this needs to be a string so let's make
it as string, okay. Let's make this it'll be readable. There we go. Okay and
now we do have our URL as a string and yeah let's create a basic URL because
with when we are going to fetch our image we want to have or insert a URL.
So let's do that right now. So let and rather yet let's name this
URL string because that is what it is. It is a string and let's add in here our
URL equals URL and we will construct this with a string and that is URL
string. There we go. Let's add this to on a new line. Yeah a lot of guard let
unwrapping but now we do have our URL. Now it's time to show it on our
UI image view. But for that we need to download it and that is why we are going
to use Kingfisher. So let's add our image with the help of kingfisher. So type in
self image view I believe we need to add in KF for Kingfisher and set image and
we are going to use the one with resource and place holder and so on,
so we do get back this completion handler. Okay, first of all let's create our
resource. So let resource equals and it is an image resource and we are going to
construct it with the download URL and let's add in there our URL that we have
just constructed. Let's copy and paste this resource and add it there. We are
not going to add any placeholders or progress blocks or anything like that
and just delete those and then it returned in the completion Handler and
as you can see Kingfisher is already using result, that is
awesome. So let's type in here result and now we can use this result within a
switch statement. So switch result and let's just wait for an Xcode complain
and we will just simply the missing statements with a fix, okay. There we go.
Let's delete that. Now with this unknown default let's talk about that this is
another Swift 5 new thing element. An unknown default is basically a default
value that is still unknown but let me just put it this way if Apple adds in
some other cases then in the compiler when you build and run you will see a
warning that okay a new case has been added here. So yeah we could just simply
add in here a print statement like so like ... Something like that and you
better take care of it, okay. Upon failure we are going to have an
error. So let error and basically we are just simply going to show the alert and
yeah that's it. Let's use that in there. Now that we do have our success we
basically can just simply present our alert with a success. So let me just
paste that in there again and just change this. Success and the message
let's see here and I believe that is it. Let's see let's just build and run and
see if all of this works. So let's hit command B make sure your device is
plugged in or rather yet for this experiment we can also use our simulator
but hey why not we already have this plugged in. So let's take a new image
here and I will take an image of my screen now and let's use
this photo. And now let's upload it first. Just wait for that and successfully okay
and now you can see that the image has been cleared from the image view.
Let's download it. There we go. We have our activity indicator and success.
Successfully downloaded image from database. There we go. We can tap okay and
there we have our image, great. That is it. Yeah, now let's just double check that we
do have in this in the database and we do have three. There we go. Let's see here.
We do have these I don't really know which one is it but that's the whole
point of a database, okay. So anonymous has all of this data if somebody just
joins they can't make it up in, they can't we make sense of it. okay.
And here we go we have three images. Now that is it for this tutorial series and this
UIImage tutorial series you have learned how you can take, save a UI image into
your phones, in photo library and of course create a custom folder for it.
You have learned how to set up firebase. Basically with firebase storage and
firestore, so you can upload your image to firebases storage and then save some
data about download URL in a fire store and then you can use that data to
download that image with a help of course of some awesome API from firebase.
Okay, now I hope you enjoyed this video and I hope you enjoyed this whole series if
you did go ahead and give it a thumbs up subscribe to the channel to see more
videos like this and maybe that notification bell to get notified of new
videos and as always I will see you in the next one.