Skip to content

Instantly share code, notes, and snippets.

@patbonecrusher
Created October 14, 2014 14:49
Show Gist options
  • Save patbonecrusher/667e51bf6a632a128629 to your computer and use it in GitHub Desktop.
Save patbonecrusher/667e51bf6a632a128629 to your computer and use it in GitHub Desktop.
Xamarin CGContext wrapper enabling you to the context in a using statement
using System;
using MonoTouch.CoreGraphics;
namespace Utilities
{
public class CGContextHelper : IDisposable
{
private readonly CGContext _context;
private bool _disposed;
public CGContextHelper(CGContext context) {
_context = context;
_context.SaveState();
}
#region IDisposable implementation
public void Dispose ()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_context.RestoreState();
}
}
_disposed = true;
}
#endregion
}
}
using (var ctx = new CGContextHelper(UIGraphics.GetCurrentContext())) {
// Your context drawing code goes here. No need to call save or restore context, it will automatically be taken care off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment