Use idevicescreenshot for taking screenshots on iOS


#1

idevicescreenshot tool takes less 1 second to capture screenshot on iOS device, however average screenshot size is ~3 mb, but after converting to .png it will be less than 1 mb.


IOS screenshot not complete with Appium 1.4.13
#2

Hi @kirill
can you provide the steps or code sample if you have one.


#3

Sure. Here is a code on Ruby:

`idevicescreenshot -u #{$device_udid} temp.tiff`
`sips -s format png temp.tiff --out #{screenshot_path}`
`rm ./temp.tiff`

where $device_udid is UDID of iOS device. screenshot_path is path where I save screenshot (should include extension: ../screenshot.png for example).

Or you can do that easily on Shell:

idevicescreenshot -u $1 temp.tiff
sips -s format png temp.tiff --out screenshot.png
rm ./temp.tiff

where $1 will be UDID of the device passed as parameter.


#4

Hi Kirill,

Can you please share the code in Java…

As I don’t know Ruby…

Thanking you,
bhaskar


#5

I use java Runtime class to execute system commands. You can use it like :

Runtime.getRuntime().exec("idevicescreenshot -u "+udid+" temp.tiff");

Another way of use with two commands. no need to use temp file.

idevicescreenshot -u $1 screenshot.png
sips -s format png screenshot.png --out screenshot.png

#6

Hi Suaylpozmen,

Thx for the support. I tried the code which you have shared with me. But when I am trying to run this code in Eclipse, I am getting a runtime error as “Cannot run program “idevicescreenshot”: error=2, No such file or directory”.

I am using the below code:

Runtime.getRuntime().exec("idevicescreenshot -u "+ “b46d35609a2a9e445c8d1faa7c184d93ecc576c7” + “/Users/bhaskar/Downloads/First.tiff”);

Can you pls help me in solving the issue.

Thanking you,
Bhaskar.M


#7

you need to install libimobiledevice from brew.


#8

@bhaskar123 can you run idevicescreenshot from command line? Eclipse could not find the binary in PATH.


#9

Hi Suaylpozmen,

You want me to run the below code in command line:

Runtime.getRuntime().exec("idevicescreenshot -u "+ “b46d35609a2a9e445c8d1faa7c184d93ecc576c7” + “First.tiff”);

when I am running this code in command line, I am getting an syntax error for .exec


#10

Hi Pr4bh4sh,

I have already installed libimobiledevice in my machine. Even though I am unable to take screenshot.

Thanking you,
Bhaskar.M


#11

@bhaskar123 no you should only run idevicescreenshot command in terminal :slight_smile: not the java code.
if it is okay you have to add path variable to eclipse configuration.


#12

Thq, I am able to take screenshot using idevicescreenshot from command line successfully.

Suaylpozem,

I am new to Appium and as well as Java code, Can you help me in writing code to add path variable to eclipse. So that I can take screenshots on real device.


#13

Add /bin/bash -l -c before the command it will work. Adding the variable is not required with this step.


#14

Hi Friend,

Thanks for the answer. But I am unable to get you how i can using /bin/bash -l -c command and run eclipse to take screenshot on real iOS device.

Can you please explain me clearly.

Thanking you,
Bhaskar.M


#15

If you are executing the command from java process builder then adb devices should be like.
/bin/bash -l -c adb devices


#16

Hi Pr4bh4sh,

I have used the below code, and I am able to get screenshot in Tiff format.
“Runtime.getRuntime().exec(”/bin/bash -l -c idevicescreenshot");"

But I want screen shot in jpg. So that I can attach this screenshot to “Extent Reports”

So how can I take screenshot in jpg format.

Thanking you,
Bhaskar.M


#17

idevicescreenshot original_screenshot.tiff
sips -s format png original_screenshot.tiff --out converted_screenshot.png


#18

with Java:

public void saveScreenToJPGFile(BufferedImage image, String fileUrl) {
        try {
            // create a blank, RGB, same width and height, and a white background
            BufferedImage newBufferedImage = new BufferedImage(image.getWidth(),
                    image.getHeight(), BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);

            ImageOutputStream ios = ImageIO.createImageOutputStream(new File(fileUrl));
            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
            ImageWriter writer = iter.next();
            ImageWriteParam iwp = writer.getDefaultWriteParam();
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwp.setCompressionQuality(0.90F);
            writer.setOutput(ios);
            writer.write(null, new IIOImage(newBufferedImage, null, null), iwp);
            writer.dispose();

            System.out.println("   screenshot saved: " + fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

#19

Hi pr4bh4sh,

I am able to take screenshot using this code

“sips -s format png original_screenshot.tiff --out converted_screenshot.png”

But I am unable to open the converted screenshot in web application. I am getting the below error message

"This image cannot be displayed because it contains errors".

can you help me…


#20

Not sure what would be the cause, can you try taking the image and then converting it to png, using the example @Aleksei has provided.

To identify the root cause try opening the tiff image without converting it. If you see the image means there’s nothing wrong with file creation and something going wrong with sips conversion(which is unlikely).