Created
November 5, 2015 16:30
-
-
Save ppamorim/cc79170422236d027b2b to your computer and use it in GitHub Desktop.
Add padding/margin at a image!
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 | |
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
Thank you! It very useful.