1

作为注册用户,我是 Stackoverflow 的新手,但它在学习 Cocoa 的开始方面帮助了我很多。但是,有一个问题我无法借助现有的主​​题/问题来解决,所以我决定自己在这里问一个问题。

我正在尝试将屏幕捕获与 NSTask 一起使用,以下代码适用于我的应用程序:

NSArray *args = [NSArray arrayWithObjects: @"-c", nil];
[[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" arguments: args] waitUntilExit];

它将屏幕截图保存到我的剪贴板,但是,我传递给 screencapture 的所有其他参数都不起作用。相同的代码在终端中确实有效。

例如,如果我screencapture -M mailme.jpg进入终端,则会打开一条新邮件消息(与保存到根文件夹或桌面相同)。在我的应用程序中它是行不通的。

以下代码:

NSArray *args = [NSArray arrayWithObjects: @"**-M mailme.jpg**", nil];
[[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" arguments: args] waitUntilExit];

结果如下:

screencapture: illegal option --  
usage: screencapture [-icMPmwsWxSCUtoa] [files]
  -c         force screen capture to go to the clipboard
  -C         capture the cursor as well as the screen. only in non-interactive modes
  -d         display errors to the user graphically
  -i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot
  -m         only capture the main monitor, undefined if -i is set
  -M         screen capture output will go to a new Mail message
  -o         in window capture mode, do not capture the shadow of the window
  -P         screen capture output will open in Preview
  -s         only allow mouse selection mode
  -S         in window capture mode, capture the screen not the window
  -t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
  -T<seconds> Take the picture after a delay of <seconds>, default is 5
  -w         only allow window selection mode
  -W         start interaction in window selection mode
  -x         do not play sounds
  -a         do not include windows attached to selected windows
  -r         do not add dpi meta data to image
  -l<windowid> capture this windowsid
  -R<x,y,w,h> capture screen rect
  files   where to save the screen capture, 1 file per screen

当我test.png用作参数时,会出现与终端中相同的输出:

libpng warning: zero length keyword
libpng warning: Empty language field in iTXt chunk

,但没有保存文件。

这是否意味着权限有问题?我应该将输出保存在我的应用程序中吗?喜欢:

if ([task terminationStatus] == 0)
 {

 }

我尝试了很多方法,但我希望/怀疑这个问题有一个非常简单的解决方案,我只是没有看到。

提前感谢您的时间和帮助。

弗兰斯

4

1 回答 1

2

我认为问题在于单个参数"-M mailme.jpg"

如果将其分成 2 个参数,它应该可以工作,即:

NSArray *args = [NSArray arrayWithObjects: @"-M", @"mailme.jpg", nil];
于 2012-12-04T13:11:13.190 回答