Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Last active December 11, 2015 20:39
Show Gist options
  • Save prashantvc/4657263 to your computer and use it in GitHub Desktop.
Save prashantvc/4657263 to your computer and use it in GitHub Desktop.
Extension method to convert UIView to UIImage
public static class Extensions
{
public static UIImage ToImage (this UIView view)
{
RectangleF canvasRect = view.Bounds;
UIGraphics.BeginImageContextWithOptions (canvasRect.Size, false, 0.0f);
CGContext ctx = UIGraphics.GetCurrentContext ();
ctx.FillRect (canvasRect);
view.Layer.RenderInContext (ctx);
UIImage newImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
NSData imgData = newImage.AsPNG ();
return UIImage.LoadFromData (imgData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment