public
Created — forked from praeclarum/UIKitExportEnv.cs

Sometimes, exporting images isn't easy

  • Download Gist
UIKitExportEnv.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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);
}
}
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.