Skip to content

Instantly share code, notes, and snippets.

@mihai-salari
Forked from ppamorim/ImageInsets.swift
Created October 9, 2017 10:13
Show Gist options
  • Save mihai-salari/4e1963ac10c406a16072b9c4a8f39b0d to your computer and use it in GitHub Desktop.
Save mihai-salari/4e1963ac10c406a16072b9c4a8f39b0d to your computer and use it in GitHub Desktop.
Add padding/margin at a image!
import UIKit
extension UIImage {
func imageWithInsets(insetDimen: CGFloat) -> UIImage {
return imageWithInset(UIEdgeInsets(top: insetDimen, left: insetDimen, bottom: insetDimen, right: insetDimen))
}
func imageWithInset(insets: UIEdgeInsets) -> UIImage {
UIGraphicsBeginImageContextWithOptions(
CGSizeMake(self.size.width + insets.left + insets.right,
self.size.height + insets.top + insets.bottom), false, self.scale)
let origin = CGPoint(x: insets.left, y: insets.top)
self.drawAtPoint(origin)
let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageWithInsets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment