iPhone模拟器上的自动屏幕截图?

问题描述:

我已经厌倦了每次换我的iPhone应用程序的用户界面时都会拍摄新的截图。我希望能够运行一个脚本/程序/无论在模拟器上加载我的二进制文件,然后做一些截图。iPhone模拟器上的自动屏幕截图?

该解决方案可以使用任何语言......对我无关紧要。

谢谢!

+2

我想听到从设备上做到这一点的答案。 – 2009-09-01 04:51:17

使用iPhone SDK 4,您可以自动执行GUI测试,并且可以为您拍摄屏幕截图。

基本上,你写一个JavaScript脚本,然后仪器(使用自动化模板)可以在设备上运行它来测试UI,并且可以记录数据,截图等,并且如果有什么东西被破坏也可以提醒。

我找不到它的参考指南,但在SDK参考库中搜索UIA*类(如UIAElement)。

还有里面的iPhone模拟器从WWDC demoing这个视频,会话306

+1

以下是Apple的参考文档:https:/ /developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Built-InInstruments/Built-InInstruments.html#//apple_ref/doc/uid/TP40004652-CH6-SW75 – 2011-12-13 20:34:26

私人UIGetScreenImage(void) API可用于捕获屏幕的内容:

CGImageRef UIGetScreenImage(); 
void SaveScreenImage(NSString *path) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    CGImageRef cgImage = UIGetScreenImage(); 
    void *imageBytes = NULL; 
    if (cgImage == NULL) { 
     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
     imageBytes = malloc(320 * 480 * 4); 
     CGContextRef context = CGBitmapContextCreate(imageBytes, 320, 480, 8, 320 * 4, colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big); 
     CGColorSpaceRelease(colorspace); 
     for (UIWindow *window in [[UIApplication sharedApplication] windows]) { 
      CGRect bounds = [window bounds]; 
      CALayer *layer = [window layer]; 
      CGContextSaveGState(context); 
      if ([layer contentsAreFlipped]) { 
       CGContextTranslateCTM(context, 0.0f, bounds.size.height); 
       CGContextScaleCTM(context, 1.0f, -1.0f); 
      } 
      [layer renderInContext:(CGContextRef)context]; 
      CGContextRestoreGState(context); 
     } 
     cgImage = CGBitmapContextCreateImage(context); 
     CGContextRelease(context); 
    } 
    NSData *pngData = UIImagePNGRepresentation([UIImage imageWithCGImage:cgImage]); 
    CGImageRelease(cgImage); 
    if (imageBytes) 
     free(imageBytes); 
    [pngData writeToFile:path atomically:YES]; 
    [pool release]; 
} 

一定要给它一个#ifdef内,因此不会出现在发布版本。

+0

我无法让方法UIGetScreenImage工作。 – 2009-09-10 15:15:08

+0

错误是什么? – rpetrich 2009-09-11 05:23:58

+0

似乎UIGetScreenImage不再适用于模拟器:((最后我试着在2.x上)我已经更新了代码以包含手动屏幕捕捉的基础知识......它似乎在纵向窗口中工作得很好,如果你改进它,请更新我的答案:) – rpetrich 2009-09-11 06:54:57

我有同样的愿望。我希望能够保存我的应用程序中几个屏幕的屏幕截图,而无需进行所有手动工作。我还没到,但我已经开始了。

这个想法是尾部/var/log/system.log,从NSLog语句的输出去。我将输出输出到一个python程序。 python程序读取stdin中的所有行,当行匹配特定模式时,它会调用screencapture。

NSLog(@"screenshot mainmenu.png"); 

这将导致每次调用时都会创建一个名为“XX。mainmenu YY.png”的屏幕截图。 XX是自程序启动以来屏幕截图的编号。 YY是“mainmenu”屏幕截图的编号。

我甚至增加了一些不必要的功能:

NSLog(@"screenshot -once mainmenu.png"); 

这只会保存 “XX mainmenu.png。” 一次。

NSLog(@"screenshot -T 4 mainmenu.png"); 

这将使屏幕截图延迟4秒后。

运行与正确的记录应用后,可能已创建具有以下名称的截屏:

00. SplashScreen.png 
01. MainMenu 01.png 
03. StartLevel 01.png 
04. GameOver 01.png 
05. MainMenu 02.png 

试试看:

  1. 一些的NSLog语句添加到您的代码

  2. $ tail -f -n0 /var/log/system.log | ./grab.py

  3. 在模拟器

  4. 玩弄你的应用程序开始你的iPhone应用程序

  5. 看看截图显示了在您启动grab.py程序

抢。潘岳:

#!/usr/bin/python 

import re 
import os 
from collections import defaultdict 

def screenshot(filename, select_window=False, delay_s=0): 
    flags = [] 
    if select_window: 
     flags.append('-w') 
    if delay_s: 
     flags.append('-T %d' % delay_s) 
    command_line = 'screencapture %s "%s"' % (' '.join(flags), filename) 
    #print command_line 
    os.system(command_line) 

def handle_line(line, count=defaultdict(int)): 
    params = parse_line(line) 
    if params: 
     filebase, fileextension, once, delay_s = params 
     if once and count[filebase] == 1: 
      print 'Skipping taking %s screenshot, already done once' % filebase 
     else: 
      count[filebase] += 1 
      number = count[filebase] 
      count[None] += 1 
      global_count = count[None] 
      file_count_string = (' %02d' % number) if not once else '' 

      filename = '%02d. %s%s.%s' % (global_count, filebase, file_count_string, fileextension) 
      print 'Taking screenshot: %s%s' % (filename, '' if delay_s == 0 else (' in %d seconds' % delay_s)) 
      screenshot(filename, select_window=False, delay_s=delay_s) 

def parse_line(line): 
    expression = r'.*screenshot\s*(?P<once>-once)?\s*(-delay\s*(?P<delay_s>\d+))?\s*(?P<filebase>\w+)?.?(?P<fileextension>\w+)?' 
    m = re.match(expression, line) 
    if m: 
     params = m.groupdict() 
     #print params 
     filebase = params['filebase'] or 'screenshot' 
     fileextension = params['fileextension'] or 'png' 
     once = params['once'] is not None 
     delay_s = int(params['delay_s'] or 0) 
     return filebase, fileextension, once, delay_s 
    else: 
     #print 'Ignore: %s' % line 
     return None 

def main(): 
    try: 
     while True: 
      handle_line(raw_input()) 
    except (EOFError, KeyboardInterrupt): 
     pass 

if __name__ == '__main__': 
    main() 

与此版本的问题:

如果你想只取iPhone模拟器窗口的截图,你必须点击iPhone模拟器窗口每截图。 screencapture拒绝捕获单个窗口,除非您愿意与它交互,这是命令行工具的一个奇怪的设计决策。

更新:现在iPhone模拟器裁切器(在http://www.curioustimes.de/iphonesimulatorcropper/index.html)从命令行工作。因此,不要使用内置的screencapture,而应该下载并使用它。所以现在这个过程是完全自动的。

+2

这种技术的一个主要问题是,它需要雇用人员屏幕才能工作。我在没有外部监视器的情况下在15英寸的MBP上进行了开发,并且此工具无法捕获iPad大小的屏幕截图。有关如何在没有更大屏幕的情况下自动执行此操作的任何建议? – radven 2010-12-20 18:32:02

,有一个“复制屏幕”菜单项。当您按住Control键时,它会取代编辑菜单中的复制菜单项。 按键是Ctrl-Cmd-C 一个简单的AppleScript可以复制一个ScreenShot并保存它。喜欢的东西(它的工作对我来说,即使是哈克):

tell application "iPhone Simulator" to activate 
tell application "System Events" 
    keystroke "c" using {command down, control down} 
end tell 
tell application "Preview" to activate 
tell application "System Events" 
    keystroke "n" using {command down} 
    keystroke "w" using {command down} 
    delay 1 
    keystroke return 
    delay 1 
    keystroke "File Name" 
    keystroke return 
end tell 

如果你不明白这一点,请评论...

您也可以使用一些屏幕捕获应用程序在模拟器的屏幕上捕捉视频。

我使用的应用程序很多。

我甚至用它来发送呈现应用程序的客户端视频......

如果你有兴趣的自动化自动改变仿真语言和设备类型,我开发了一些脚本,这样做:http://github.com/toursprung/iOS-Screenshot-Automator