Created
October 14, 2014 14:49
-
-
Save patbonecrusher/667e51bf6a632a128629 to your computer and use it in GitHub Desktop.
Xamarin CGContext wrapper enabling you to the context in a using statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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