Skip to content

Instantly share code, notes, and snippets.

@KiGi
Created December 21, 2015 02:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save KiGi/c6ddba8ed2276773c9fa to your computer and use it in GitHub Desktop.
Draw UIView into image from static class method
static func buildImageForSize(size:CGSize) -> UIImage?
{
if size.height == 0 || size.width == 0 {
return .None
}
var sigRect = CGRectZero
sigRect.size = size
// The following does NOT work
//let space = CGColorSpaceCreateDeviceRGB();
//let ctx = CGBitmapContextCreate(nil, Int(size.width), Int(size.height), 8, Int(size.width) * 4, space, CGImageAlphaInfo.PremultipliedLast.rawValue);
UIGraphicsBeginImageContextWithOptions(size, false , 1.0)
let ctx = UIGraphicsGetCurrentContext()
let useView = UIView(frame: sigRect)
useView.backgroundColor = UIColor.redColor()
// Convert populated signature view to UIImage
// There is no real current context, so make one so we can turn the view into an image
var image : UIImage? = .None
if let context = ctx {
useView.layer.renderInContext(context)
image = UIGraphicsGetImageFromCurrentImageContext()
}
UIGraphicsEndImageContext()
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment