How to take a screenshot programmatically on iOS

前端 未结 20 2073
一生所求
一生所求 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<nButtons; i++) {
        UIALogger.logMessage("button " + buttons[i].name());
    }
    
    var tabBar = window.tabBars()[0];
    var barButtons = tabBar.buttons();
    
    var nBarButtons = barButtons.length;
    UIALogger.logMessage(nBarButtons + " buttons on tab bar");
    
    for (var i=0; i<nBarButtons; i++) {
        UIALogger.logMessage("button " + barButtons[i].name());
    }
    
    var weeksButton = barButtons[1];
    var monthsButton = barButtons[2];
    var yearsButton = barButtons[3];
    
    target.delay(2);
    weeksButton.tap();
    target.captureScreenWithName("daysShot2");
    target.delay(2);
    monthsButton.tap();
    target.captureScreenWithName("daysShot3");
    target.delay(2);
    yearsButton.tap();
    target.delay(2);
    button2.tap();
    target.delay(2);
    setYear(picker, 2018);
    target.delay(2);
    target.captureScreenWithName("daysShot4");
    
    0 讨论(0)
  • 2020-11-22 02:16

    For iOS 7.0 or above..

    If you want to take screenshots of view say(myView), you can do this with single line:

    [myView snapshotViewAfterScreenUpdates:NO];
    
    0 讨论(0)
  • 2020-11-22 02:17

    See this post it looks like you can use UIGetScreenImage() for now.

    0 讨论(0)
  • 2020-11-22 02:18

    This will save a screenshot and as well return the screenshot too.

    -(UIImage *)capture{
        UIGraphicsBeginImageContext(self.view.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(imageView, nil, nil, nil); //if you need to save
        return imageView;
    }
    
    0 讨论(0)
  • 2020-11-22 02:18

    In Swift you can use following code.

    if UIScreen.mainScreen().respondsToSelector(Selector("scale")) {
        UIGraphicsBeginImageContextWithOptions(self.window!.bounds.size, false, UIScreen.mainScreen().scale)
    }
    else{
        UIGraphicsBeginImageContext(self.window!.bounds.size)
    }
    self.window?.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image : UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    
    0 讨论(0)
  • 2020-11-22 02:21

    Below method works for OPENGL objects also

    //iOS7 or above
    - (UIImage *) screenshot {
    
        CGSize size = CGSizeMake(your_width, your_height);
    
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
        CGRect rec = CGRectMake(0, 0, your_width, your_height);
        [_viewController.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    0 讨论(0)
提交回复
热议问题