Inset image in UIImageView

I have a UIImageView in which I want to put an image. By default the image in the imageView will go edge to edge and cover the entire imageView. What I want to do is inset or "shrink" the image so that it sits inside the imageView with a nice padding. Any guidance on an approach for this would be appreciated! Thanks in advanced..


Please see screenshot for intended output: h ttps://drive.google.com/open?id=1IxJ48By22t-7qHBOf5zuKpT6MzaaNPVT

Accepted Answer

The easy way to do this is to design the inset into the image itself. Or, you can construct a second image programmatically where you draw the first image into a larger area.


If you really want to use the image unchanged, you would need to put the UIImageView into a larger container, so that the insets are outside the image view, not inside the image view. You would then adjust the shape and background of the container view, not of the image view.


Personally, I'd go for designing the inset into the image. Since the amount of the inset is likely as much a design decision as the rest of the image, it makes sense to lock it into the design.

How many images?


Have you considered masking the image with transparency, shifting it to one side by adding margins?

I thought of already doing your first suggestion and I agree with the premise of designing the inset in the image creation itsefl... however, after some careful deliberations internally, we need to have a mechanism that will shrink any image that we provide programatically...


> If you really want to use the image unchanged, you would need to put the UIImageView into a larger container, so that the insets are outside the image view, not inside the image view. You would then adjust the shape and background of the container view, not of the image view.


Is there a more simpiler solution to this... like would writing an extension for UIImage be the way to go... using UIEdgeInsets?


Thanks!




QuinceyMorris... I thought about it again and I agree with you suggestion designing the inset into the image... this makes life a whole lot easier. Thanks! 🙂

Inset image in UIImageView
 
 
Q