Created
January 19, 2020 03:19
-
-
Save stleamist/926cf6d36f4453dcfb8c2b5e1433b247 to your computer and use it in GitHub Desktop.
This file contains 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 resizeCanvas(size canvasSize: CGSize, color canvasColor: UIColor = .clear) -> UIImage { | |
let renderer = UIGraphicsImageRenderer(size: canvasSize) | |
let resizedImage = renderer.image { (context) in | |
canvasColor.setFill() | |
context.fill(CGRect(origin: .zero, size: canvasSize)) | |
let selfOrigin = CGPoint( | |
x: (context.format.bounds.width - self.size.width) / 2, | |
y: (context.format.bounds.height - self.size.height) / 2 | |
) | |
self.draw(at: selfOrigin) | |
} | |
return resizedImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment