Created
August 5, 2022 09:52
-
-
Save eleev/d10f0d8be90468d2c631d7eba7539bc7 to your computer and use it in GitHub Desktop.
UIImage Inset - blank space around a UIImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public extension UIImage { | |
func image(uniformInset inset: CGFloat) -> UIImage? { | |
image(with: | |
UIEdgeInsets( | |
top: inset, | |
left: inset, | |
bottom: inset, | |
right: inset | |
) | |
) | |
} | |
func image(with insets: UIEdgeInsets) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions( | |
CGSize(width: size.width + insets.left + insets.right, | |
height: size.height + insets.top + insets.bottom), | |
false, | |
scale | |
) | |
let origin = CGPoint(x: insets.left, y: insets.top) | |
draw(at: origin) | |
guard let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext() else { | |
return nil | |
} | |
UIGraphicsEndImageContext() | |
return imageWithInsets | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment