Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems.

Hi guys,

I’m trying to take a screenshot and then show it on an Image using Sprite.Create.
This works perfectly on both PC and Android, but on iOS it only puts part of the screen on the image.
How can I fix that?

Texture2D tex = new Texture2D(Screen.width,(int)((1672 / 1920f) * Screen.height));
tex.ReadPixels(new Rect(0, 0, Screen.width,(int)((1672 / 1920f) * Screen.height)), 0, 0);
tex.Apply();
img.gameObject.SetActive (true);
img.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));

Thanks in advance,

Arseney

I couldn’t tell you why it’s only putting on a part of the texture, but you could try using Application.CaptureScreenshot() to take the screenshot, the problem is that this saves it as a file rather than returning a texture or sprite, but you could then use the WWW class to load this file (Which is saved to the PersistentDataPath on iOS)

void GetScreenShot(){
     Application.CaptureScreenshot("Screenshot.png");
     StartCoroutine(LoadImage());
}
Enumerator LoadImage(){
     WWW www = new WWW("File://"+Application.persistentDataPath+"/Screenshot.png");

     yield return www;

     Texture2D tex = www.texture;

    // and I'm sure you can handle the rest!
}

You’ll need to create a sprite with the WWW.texture and voila.

I’ve not tried this in unity so apologies if you need to correct some things, the “File://” differs on some platforms I believe, but I think this right for iOS, see the WWW documentation for more details.

I’ve never used Application.CaptureScreenshot before, you may find that it takes time to save, and calling the WWW straight away may not work, so you’ll need to wait.

I really hope this helps and looking forward to hearing if it works! :slight_smile:

Beau C