How to take a screenshot programmatically on iOS

前端 未结 20 2366
一生所求
一生所求 2020-11-22 01:42

I want a screenshot of the image on the screen saved into the saved photo library.

20条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:14

    Another option is to use the Automation tool on instruments. You write a script to put the screen into whatever you state you want, then take the shots. Here is the script I used for one of my apps. Obviously, the details of the script will be different for your app.

    var target = UIATarget.localTarget();
    var app = target.frontMostApp();
    var window = app.mainWindow();
    var picker = window.pickers()[0];
    var wheel = picker.wheels()[2];
    var buttons = window.buttons();
    var button1 = buttons.firstWithPredicate("name == 'dateButton1'");
    var button2 = buttons.firstWithPredicate("name == 'dateButton2'");
    
    function setYear(picker, year) {
        var yearName = year.toString();
        var yearWheel = picker.wheels()[2];
        yearWheel.selectValue(yearName);
    }
    
    function setMonth(picker, monthName) {
        var wheel = picker.wheels()[0];
        wheel.selectValue(monthName);
    }
    
    function setDay(picker, day) {
        var wheel = picker.wheels()[1];
        var name = day.toString();
        wheel.selectValue(name);
    }
    
    target.delay(1);
    setYear(picker, 2015);
    setMonth(picker, "July");
    setDay(picker, 4);
    button1.tap();
    setYear(picker, 2015);
    setMonth(picker, "December");
    setDay(picker, 25);
    
    target.captureScreenWithName("daysShot1");
    
    var nButtons = buttons.length;
    UIALogger.logMessage(nButtons + " buttons");
    for (var i=0; i

提交回复
热议问题