ImageView insect in iOS swift || Imageview with space in all sides iOS

Problem:

I want to create an imageView with image and it will not be filled with imageView. 

It wants to be like four sides wants to be few space needed to show.

So i tried to give imageview.contentmode = .center

But it shows that more space came in imageview but image is in small in center of imageview

After that i had tried to give border color but it also a bad solution for that.

How can i achieve that?

Solution:

extension UIImage {

    

    func imageInsets(insetsUIEdgeInsets) -> UIImage? {

           UIGraphicsBeginImageContextWithOptions(

               CGSize(widthself.size.width + insets.left + insets.right,

                      heightself.size.height + insets.top + insets.bottom), falseself.scale)

           let _ = UIGraphicsGetCurrentContext()

           let origin = CGPoint(x: insets.lefty: insets.top)

           self.draw(at: origin)

           let imageInsets = UIGraphicsGetImageFromCurrentImageContext()

           UIGraphicsEndImageContext()

           return imageInsets

       }

}


added the extension for image in extension file and used as like below.


 imgView.image = imgView.image!.imageInsets(insetsUIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5))


Wow i had achieve that..It's working perfectly what i want...Happy coding..

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift