public void Save(string path, ImageFormat format)
{
if (path == null)
throw new ArgumentNullException ("path");
if (NativeCGImage == null)
throw new ObjectDisposedException ("cgimage");
// With MonoTouch we can use UTType from CoreMobileServices but since
// MonoMac does not have that yet (or at least can not find it) I will
// use the string version of those for now. I did not want to add another
// #if #else in here.
// for now we will just default this to png
var typeIdentifier = "public.png";
// Get the correct type identifier
if (format == ImageFormat.Bmp)
typeIdentifier = "com.microsoft.bmp";
// else if (format == ImageFormat.Emf)
// typeIdentifier = "image/emf";
// else if (format == ImageFormat.Exif)
// typeIdentifier = "image/exif";
else if (format == ImageFormat.Gif)
typeIdentifier = "com.compuserve.gif";
else if (format == ImageFormat.Icon)
typeIdentifier = "com.microsoft.ico";
else if (format == ImageFormat.Jpeg)
typeIdentifier = "public.jpeg";
else if (format == ImageFormat.Png)
typeIdentifier = "public.png";
else if (format == ImageFormat.Tiff)
typeIdentifier = "public.tiff";
else if (format == ImageFormat.Wmf)
typeIdentifier = "com.adobe.pdf";
// Not sure what this is yet
else if (format == ImageFormat.MemoryBmp)
throw new NotImplementedException("ImageFormat.MemoryBmp not supported");
// Obtain a URL file path to be passed
NSUrl url = NSUrl.FromFilename(path);
// * NOTE * we only support one image for right now.
// Create an image destination that saves into the path that is passed in
CGImageDestination dest = CGImageDestination.Create (url, typeIdentifier, frameCount);
// Add an image to the destination
dest.AddImage(NativeCGImage, (NSDictionary)null);
// Finish the export
bool success = dest.Close ();
// if (success == false)
// Console.WriteLine("did not work");
// else
// Console.WriteLine("did work: " + path);
dest.Dispose();
dest = null;
}