Hi Alex here from rebeloper.com and welcome back to the channel and welcome back to
the second video on our series on how to save, take, download and upload UI images
in iOS and Swift 5. Now in the previous episode we have talked how you can save
an image into your photo library. Today we are going to talk about how you can
save it in a separate dedicated folder so without further ado let's jump right into it.
Okay, let me just build and run this project and show you or rather yet
let us just add in here some more code and after we have
unwrapped our image I will just simply start the activity indicator.
startAnimating( ). Let's just build and run if and if you are new here go ahead and
take a look at the first video. What we are trying to achieve here let's
unlock our device because we are using our device... we are taking a photo so
there we go. Let's take a close photo of... there we go!
That's me there and let's use this photo and now when we tap on save we should be
able to see the activity indicator till we get the alert, UIAlert that we have a
save photo. Now there's too much dust on my computer screen... Let's hit save
there and Image saved successfully. It was kind of instant but let's hit OK there
and let's just double check if we do have that there we go! We have saved that
into our photos library. Great! Today what we want to do is create an album and
then save it to our photo library. Well get this
kind of hard to do and code out. So lucky for you I have created a class for that
and go ahead and click the link down below, if you haven't already done so to
see your resources and there you will see a CustomerAlbum.swift file and
an Error+.swift file. If you don't know about custom errors go ahead and
take a look at the file and at my video on how to create your very own custom
errors. Okay, so that's basically it. What we want to do is drag both of these
into Xcode. Let's just add that in there and now we are able to have the
power of this CustomAlbum and as object. It's class basically then we can
create our albums. Let's go back and let's uncomment this UIImageWriteToSavePhotosAlbum
and what we're basically want to use is saveToAlbum.
This function so let's add that in here. SaveToAlbum and the name will
be lets say "Swift 5 UIImage" or whatever you would like that to be
called. And image we we provide is the image that we have unwrapped from the
image views image. Okay, now that's it basically. When we are tapping
on Save photo we will be calling upon this saveToAlbum function.
Let's talk about this saveToAlbum. Okay, now one thing that you need to know about
this CustomAlbum.swift file is that it's very easy to use and this is how
you could do it. First of all you need to create an album: let album, there we go!
= (equals) and this will be a CustomAlbum and you can name your custom album
for whatever you would like but I am going to use this
named property that we have added to the saveToAlbum function. Now one final
thing is to grab album and type in save and there we go!
We have an image so that will be our image and we have a completion handle
with the Swift files brand new results type. Go ahead and take a look at that in
the channel because I'm covering it in depth so you may understand what this is
all about. If we tap on there we get back a result that result has a
boolean value and an error. Let's type in here: result and now let's take care
of this result. We are going to use the same old switch statement result
here and let's just wait for the auto completion to nag about this so switch
must be exhaustive. Okay, let's fix this. It's a real time-saver. Okay, now for
success we are going to leave this as is because it's just a boolean value but
for failure we are going to add in here our error. And what we want to do here is...
well we could print out the error but we are going to simply just use this
presentAlert function. Let's go there presentAlert, there we go. And the title
will be Error and message will be err.lozalizedDescription. And that takes
care of that. Now if we do have a success or let's just fix this compiler error there.
I f we do have successfully saved the image then we want to say to
the user that okay we have successfully saved this to our custom album. Now just
think about it this is handled on a background thread so most probably you
already know I want to call upon our DispatchQueue.main.
Let's do that here DispatchQueue.main.async and let's just
copy and paste the switch on to there. Okay, there we go!
Why do I want to add this first of all because of the alert but yeah maybe
I switched to the alert in later time on a DispatchQueue. But now we want to
basically stop the activity animator so activity indicator you want to stop it
and that needs to be on the main thread. Let's just create the success alert.
There we go. "Success" and now let's type in
Successfully and maybe let's add in the heaven name too. Let's go here..
rather yet let me just show you a neat little trick how you can add these
quotation marks. There we go. "named". Let's close that off that should
be okay. Okay, so basically that is it and
everything is taken care by the custom alert album class. Let's build and
run and let's see how this will work. Okay, build has succeeded that is great.
Let's wait for this to launch. Let's take a photo.
There we go! Use photo and now let's save it. So now
as you recall we have added a privacy so this app wants to use your photo library
and that is because we want to save some images. Let's hit okay there.
Successfully saved photo to album on Swift 5 UIImage. And I believe we
have some errors here some typos. Let's see there and small m. Okay fix that.
Let's go back to our QuickTime player and let's hit okay and that is it.
Now let's double check. Let's go back to our photos and if we select an albums
we should be able to see all and let's just scroll all the way down and there
we have it. Swift UIImage, it's a with a capital M
but hey that is okay for now There we go! We have our image saved in our custom folder.
Now that is it for this episode in the previous one we have
saved our image to our photo library. Basic stuff. Now we have saved it
into our custom folder. Now in the next one we are going to upload our photo and
guess where to Firebase Storage. Yes that is right. If you did like this video
go ahead and give it a thumbs up it helps a lot and subscribe to the channel
if you already haven't done so and hit that notification bell. Now it is your
turn go ahead and create your custom albums and save your photos in there and
as usual I will see you in the next one!