Search Unity

ReadPixels() Creates Black Image (Answered)

Discussion in 'iOS and tvOS' started by elveatles, Sep 1, 2010.

  1. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    So, I read through some forum posts that all say Application.CaptureScreenshot doesn't work, but ReadPixels + EncodeToPNG does.

    So, I tried this, and it works on my Mac, but on the iPhone, I get a black image. Why would that happen? I'm not very good with Objective-C so I'm guessing the problem is more likely to be with that.

    Code (csharp):
    1.  
    2. Game.library.guiCamera.farClipPlane = 0.4f;
    3.        
    4.         yield return 0; //Wait a frame for re-rendering without GUI
    5.        
    6.        
    7.         //Also take a look at [url]http://unity3d.com/support/documentation/ScriptReference/Texture2D.EncodeToPNG.html[/url]
    8.        
    9.         // We should only read the screen bufferafter rendering is complete
    10.         yield return new WaitForEndOfFrame();
    11.        
    12.         Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    13.         screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    14.         screenshot.Apply();
    15.         byte[] screenshotBytes = screenshot.EncodeToPNG();
    16.         Destroy(screenshot);
    17.        
    18.         string filePrefix = "file:///";
    19.         string savePath = Game.downloadsPath.Substring(filePrefix.Length - 1) + "screenshot.png";
    20.         File.WriteAllBytes(savePath, screenshotBytes);
    21.         ObjectiveC.TakeScreenshot();
    22.        
    23.        
    24.         Game.library.guiCamera.farClipPlane = 1000f;
    25.  
    Here's the XCode function that gets called by ObjectiveC.TakeScreenshot();

    Code (csharp):
    1.  
    2. -(void) __takeScreenshot
    3. {
    4.     NSString *imagePath = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Downloads"] stringByAppendingPathComponent:@"screenshot.png"];
    5.     UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    6.     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    7. }
    8.  
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    ReadPixels isn't supported on the iPhone.
     
  3. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    Man, that's disappointing. Thank you for replying.
     
  4. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    209
    ReadPixel() is working, and currently its working fine on iOS 5 and 6.
     
    Last edited: Nov 12, 2012
  5. tienphan

    tienphan

    Joined:
    Jan 7, 2013
    Posts:
    2
    Hi

    You could try this code
    yield return new WaitForEndOfFrame();

    Before call ReadPixel(), it is for frame to be fully rendered
     
  6. tienphan

    tienphan

    Joined:
    Jan 7, 2013
    Posts:
    2
    Or you could put ReadPixel() inside LateUpdate() method

    Regards.
     
  7. LC

    LC

    Joined:
    Jan 4, 2013
    Posts:
    2
    Hi, I using Unity3d version 4 and iOS 6.

    My code able to work on Mac and Android but not iOS6. Already tried ReadPixel(), yield return new WaitForEndOfFrame() and LateUpdate() but still not working on iOS6.

    Please advise

    Thanks
     
  8. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,541
    I use to work with GetPixels and it works perfectly on iOS.
    It require a little more work to get pixel from the give area, but nothing difficult o_<.
     
  9. LC

    LC

    Joined:
    Jan 4, 2013
    Posts:
    2
    Thanks for the reply. Juz found my problem.

    It could be due to the inappropriate Quality setting (File -> Project Setting -> Quality). Once I change the Fantastic to Simple, it works.

    Seems to me that one of the settings under Level 'Fastastic' is not supported by iOS6. Need more time to investigate which is the particular setting that is causing the problem.
     
  10. christianhess

    christianhess

    Joined:
    Dec 10, 2012
    Posts:
    3
    OMG! It really works!
    I tested it on my project. It only works if the selected Quality setting is not the Default, like in the attached image. If I set the Simple to Default, it does not work any more!

    Is it a kind of bug? And the question is whether it works on iOS5!!

    $captura.png
     
  11. Ches81

    Ches81

    Joined:
    Nov 27, 2012
    Posts:
    7
    I just found out that the Anti Aliasing causes the problem. I hotfix that by setting QualitySettings.antiAliasing = 0 in the script. Then wait 1-2 frames to make sure the application has been renderer with the new settings. Now I can take my screenshot. After that I set the AA back to what it was.

    Tested on iPhone 4 / iOS 6
     
  12. cbaltzer

    cbaltzer

    Joined:
    Jan 11, 2012
    Posts:
    120
    Beware that if you're supporting Android as well, this will crash the app. (Last tested under Unity 4.1, may be fixed now)

    Also, if you want AA in your screenshots, check out Aperture in the asset store/my signature. </shamelessplug>
     
  13. Joskym

    Joskym

    Joined:
    Oct 2, 2013
    Posts:
    39
    I submitted a bug report for the AA problem. They said that they will search for a solution. No news about it for a couple of month.
     
unityunity