Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created February 9, 2012 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save praeclarum/1775621 to your computer and use it in GitHub Desktop.
Save praeclarum/1775621 to your computer and use it in GitHub Desktop.
Sometimes, exporting images isn't easy
public class UIKitExportEnv : ExportEnv
{
public UIKitExportEnv (string name, Model model)
: base (name, model)
{
}
protected override void WriteTexture (ITexture texture, ExportedFile file)
{
var width = texture.Width;
var height = texture.Height;
using (var imageContext = texture.CreateContext ()) {
using (var image = imageContext.ToImage ()) {
using (var renderContext = texture.CreateContextWithData (IntPtr.Zero)) {
renderContext.TranslateCTM (0, height);
renderContext.ScaleCTM (1, -1);
renderContext.DrawImage (new RectangleF (0, 0, width, height), image);
using (var render = renderContext.ToImage ()) {
using (var uiImage = new UIImage (render)) {
using (var data = uiImage.AsPNG ()) {
unsafe {
using (var s = new UnmanagedMemoryStream ((byte*)data.Bytes, data.Length)) {
s.CopyTo (file.Stream);
}
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment