Skip to content

Instantly share code, notes, and snippets.

@stleamist
Created January 19, 2020 03:19
Show Gist options
  • Save stleamist/926cf6d36f4453dcfb8c2b5e1433b247 to your computer and use it in GitHub Desktop.
Save stleamist/926cf6d36f4453dcfb8c2b5e1433b247 to your computer and use it in GitHub Desktop.
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