Forum Xamarin Xamarin.iOS

How to create a screenshot from a UIView with LayerClass CAEAGLLayer

MacjonMacjon USBeta

I'm trying to create a screenshot from an EAGLView. But with the following code in my eagleview, the image is black. What am I missing?

public UIImage GetImage()
UIImage image;
var s = UIScreen.MainScreen.Scale;

        var width = (int)this.Frame.Size.Width;
        var height = (int)this.Frame.Size.Height;
        var dataLength = width * height * 4 * s * s;
        const int bitsPerComponent = 8;
        const int bitsPerPixel = 32;
        var bytesPerRow = (int)(4 * width * s);

        var buffer = new byte[(int)dataLength];

        var colorSpace = CGColorSpace.CreateDeviceRGB();

        var provider = new CGDataProvider(buffer, 0, buffer.Length);
        var bitmapInfo = CGBitmapFlags.ByteOrderDefault;
        var renderIntent = CGColorRenderingIntent.Default;

        var imageRef = new CGImage((int)(width * s), (int)(height * s), bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, renderIntent);

        UIGraphics.BeginImageContextWithOptions(new CGSize(width * s, height * s), false, s);
        var context = UIGraphics.GetCurrentContext();
        context.DrawImage(new CGRect(0, 0, width * s, height * s), imageRef);

        image = UIGraphics.GetImageFromCurrentImageContext();

        return image;

Sign In or Register to comment.