Skip to content

Instantly share code, notes, and snippets.

@eleev
Created August 5, 2022 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleev/d10f0d8be90468d2c631d7eba7539bc7 to your computer and use it in GitHub Desktop.
Save eleev/d10f0d8be90468d2c631d7eba7539bc7 to your computer and use it in GitHub Desktop.
UIImage Inset - blank space around a UIImage
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