Hi Alex here from rebeloper.com and welcome back to this tutorial series on
some awesome extensions. And today we are going to talk about a UIView extension
that can merge the elements of that UIView. So without further ado let's jump right into it.
Okay let me just show you what we are going to build out today
with the help of this TestingExtensionsFun. This is the completed project go
ahead and take a look at the link down below in the description to see the
starter project. Now here we have a really awesome image from unsplash
authored. Well we have collected this image from the site unsplash.com and
this is the author of that image. And when we are going to take a look at the code
you will see that this is a UIImageView with this image and we have a UILabel
with this text. Now I want to save all of this into a UIImage and then I'm
going to present it down here. This is also a UIImageView. First of all we
want to merge all of this, so we are just going to click on this and there you go we
have merged this. Now it doesn't really seem that anything has changed but as
you can see and you will see in the code this is a simple UIImage. And this is a
UIImage and the top of it is a UILabel. Let's take a look at how we can
achieve this. Okay! lLet's just close the testing project and take a look at the
ViewViewController. Now this is really dummy name for this View Controller but
yeah we are going to create an extension for a UIView. Here we have our image view
and our label with the text and an image a dummy image as an original
and our up button and our outputImageView. All of them are laid out with
the help of TinyConstraints and here we can handle the button tapped. Okay!
Now let's just first of all create our extension and this could lead to some
typos so I'm just going to simply paste that in and run through what basically
it does. If it's available as of iOS 10 we are going to use this one these lines
of code if not we are going to do that the old way. We are going to create a
regular that is UIGraphicsImageRenderer and we are going to render the
image on the renderer context. That is the layers renderer and so on. You don't really
need to understand all of this. What is important is that the bounce is the
bounds of our UIView. In an earlier version
of that would be to create UIGraphicsGetCurrentContext and then get the
contents frame size. Now we are not going to get bounce frame size and then we
begin an image context and here we end it. And inside there we get the renderer
and we create our image and then we guard let unwrap that image because
it's a cgImage. And we return that as a UIImage and of course we create that
UIImage with the help of the cgImage initializer. It's kind of weird but
yeah it does the job and you don't really have to care about that. Now we
have our extensions as image. Now let's go back and first of all let's create
our image. So: let image = and I believe the image is in the imageView
and as image. Now again we are adding the label to the imageView.
Now whatever is on that imageView on the bounds of that imageView will be
processed. And what we want to have now after we do that is basically add that
onto the outputImageView. So: outputImageView image = image as you can
see this is optional. And again this extension is optional too so we can
safely add that in there. Without further ado let's just start
and test this out. Oh I just forgot that we need to go to the AppDelegate and
change the example type to be view. So let's build and run again and this
should work fine. Okay! I'm back here, so let's step on merge
into image and there we go simple as that we have merged.
Of course you can add other images here and so on. I hope you enjoyed this video
merging image views and so on labels UIText and so on into simple UIImage.
If you did enjoy it go ahead and give a thumbs up subscribe to the channel and
hit that notification bell to get notified of new videos. And the next
video will be about navigation and we are going to create some awesome
navigation extensions so as usual I will see you in the next one!