对于Xcode 8.2或更高版本
您可以使用xcrun simctl
命令行实用程序控制Simulator 来拍摄Simulator的视频和屏幕截图。
- 在模拟器上运行您的应用
- 打开一个终端
运行命令
截屏
xcrun simctl io booted screenshot <filename>.<file extension>
例如:
xcrun simctl io booted screenshot myScreenshot.png
拍摄视频
xcrun simctl io booted recordVideo <filename>.<file extension>
例如:
xcrun simctl io booted recordVideo appVideo.mov
按此ctrl + c
按钮停止录制视频。
创建的文件的默认位置是当前目录。
Xcode 11.2和更高版本提供了更多选项。
从Xcode 11.2 Beta发行说明
simctl视频录制现在可以产生较小的视频文件,支持HEIC压缩,并在可用的情况下利用硬件编码支持。此外,还恢复了在iOS 13,tvOS 13和watchOS 6设备上录制视频的功能。
您可以使用其他标志:
xcrun simctl io --help
Set up a device IO operation.
Usage: simctl io <device> <operation> <arguments>
...
recordVideo [--codec=<codec>] [--display=<display>] [--mask=<policy>] [--force] <file or url>
Records the display to a QuickTime movie at the specified file or url.
--codec Specifies the codec type: "h264" or "hevc". Default is "hevc".
--display iOS: supports "internal" or "external". Default is "internal".
tvOS: supports only "external"
watchOS: supports only "internal"
--mask For non-rectangular displays, handle the mask by policy:
ignored: The mask is ignored and the unmasked framebuffer is saved.
alpha: Not supported, but retained for compatibility; the mask is rendered black.
black: The mask is rendered black.
--force Force the output file to be written to, even if the file already exists.
screenshot [--type=<type>] [--display=<display>] [--mask=<policy>] <file or url>
Saves a screenshot as a PNG to the specified file or url(use "-" for stdout).
--type Can be "png", "tiff", "bmp", "gif", "jpeg". Default is png.
--display iOS: supports "internal" or "external". Default is "internal".
tvOS: supports only "external"
watchOS: supports only "internal"
You may also specify a port by UUID
--mask For non-rectangular displays, handle the mask by policy:
ignored: The mask is ignored and the unmasked framebuffer is saved.
alpha: The mask is used as premultiplied alpha.
black: The mask is rendered black.
现在,您可以在jpeg
中使用遮罩(用于非矩形显示)和其他一些标志来截屏:
xcrun simctl io booted screenshot --type=jpeg --mask=black screenshot.jpeg