Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convenience method creating a new image by drawing into a context
import UIKit
extension UIImage
/// Creates an image from drawing into a context
convenience init(size: CGSize, opaque: Bool = true, operations: (CGContext)->())
UIGraphicsBeginImageContextWithOptions(size, opaque, 0)
let ctx = UIGraphicsGetCurrentContext()!
let image = UIGraphicsGetImageFromCurrentImageContext()!
self.init(cgImage: image.cgImage!, scale: image.scale, orientation: image.imageOrientation)
public func resized(to size: CGSize) -> UIImage
let image = UIImage(size: size, opaque: false) { context in
self.draw(in: CGRect(origin: .zero, size: size))
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment