1
\$\begingroup\$

I am trying to take a screenshot for sharing on social media. This code works on my Desktop project, but when I use it on iOS it always shows up as a white image, I am using the standard libgdx code for the screenshot factory, any suggestions?

public class ScreenShotFactory 
{
public static void SaveScreenshot()
{
    try
    {
        Gdx.files.local("shot.png").delete();
        FileHandle fh;
        do
        {
            fh = new FileHandle(Gdx.files.getLocalStoragePath() + "shot.png");
        }
        while (fh.exists());

        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

        PixmapIO.writePNG(fh, pixmap);

        pixmap.dispose();
    }

    catch (Exception e)
    {           
    }
}

private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown)
{
    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

    if (yDown) 
    {
        // Flip the pixmap upside downx
        ByteBuffer pixels = pixmap.getPixels();
        int numBytes = w * h * 4;
        byte[] lines = new byte[numBytes];
        int numBytesPerLine = w * 4;
        for (int i = 0; i < h; i++) {
            pixels.position((h - i - 1) * numBytesPerLine);
            pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
        }
        pixels.clear();
        pixels.put(lines);
        pixels.clear();
    }
    Logger.Log("ScreenShot Taken!");
    return pixmap;
 }
}
\$\endgroup\$
1
\$\begingroup\$

This post is related and offers a solution

The solution is to do the screenshot in iOS specific code:

public byte[] takeScreenshot(){
UIImage newImage;
UIGraphics.beginImageContext(new CGSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
application.getUIViewController().getView().drawViewHierarchy(
      new CGRect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()) , true);
newImage = UIGraphics.getImageFromCurrentImageContext();
UIGraphics.endImageContext();  

NSData data = newImage.toJPEGData(.6);
newImage.dispose();

return data.getBytes();
}

only problem is converting the returned data to a pixmap, which I havent figured out yet(because I dont know the format toJPEGData writes in)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.