【发布时间】:2015-05-09 15:46:05
【问题描述】:
我正在尝试从 swift 调用 gunplot 以在 .png 中生成一些图。但是,下面的程序不起作用 --- 生成的 1.png 文件是空的。但是,如果我离开“set term aqua”,它会调用带有绘图的 aqua 窗口。但是,当我尝试将输出设置为文件(png 或 pdf)时,结果始终是一个空文件。
gnuplot 命令没问题——我可以手动正确运行它们。
import Foundation
let task = NSTask()
task.launchPath = "/opt/local/bin/gnuplot"
task.currentDirectoryPath = "~"
let pipeIn = NSPipe()
task.standardInput = pipeIn
task.launch()
let plotCommand: NSString =
"set term png\n" +
"set output \"1.png\"\n"
"plot sin(x)\n" +
"q\n"
pipeIn.fileHandleForWriting.writeData(plotCommand.dataUsingEncoding(NSUTF8StringEncoding)!)
我是 swift 和管道的新手,所以如果代码有问题或不推荐,请告诉我。
【问题讨论】:
-
我不知道
swift,但如果我是你,我可以尝试: 1. 在文件名"set output \"1.png\"\n"之后添加换行符(你错过了\n) . 2:在 gnuplot 中关闭文件,在"q\n"之前添加类似"set output \n "的内容。让我知道它是否有效。 -
@Hastur 不,没有帮助。
-
我希望...只要你修好了,我们就可以放一个 + :-)
"set output \"1.png\"\n" +(我们错过了 +)。再次让我知道... -
@Hastur 天哪!!而已!
\n和+不见了,它不起作用。我想你可以给出答案。
标签: swift gnuplot nstask nspipe