Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 将NSImage输出为PDF文件 如何将NSIMAGE保存到带有基础的PDF文件?这不是GUI应用程序,因此AppKit(以及NSView)未被使用_Cocoa_Pdf - Fatal编程技术网

Cocoa 将NSImage输出为PDF文件 如何将NSIMAGE保存到带有基础的PDF文件?这不是GUI应用程序,因此AppKit(以及NSView)未被使用

Cocoa 将NSImage输出为PDF文件 如何将NSIMAGE保存到带有基础的PDF文件?这不是GUI应用程序,因此AppKit(以及NSView)未被使用,cocoa,pdf,Cocoa,Pdf,编辑:嗯,我现在觉得自己很傻。NSImage是AppKit的一部分,因此正在使用中。但是,我的问题仍然存在:如何将NSImage保存为PDF?通过连接到Windows服务器,您可以使用NSImage和NSView。您可以使用AppKit函数NSApplicationLoad与Windows服务器建立连接 main.m #include <AppKit/AppKit.h> int main(int argc, const char **argv) { if(argc != 3

编辑:嗯,我现在觉得自己很傻。NSImage是AppKit的一部分,因此正在使用中。但是,我的问题仍然存在:如何将NSImage保存为PDF?

通过连接到Windows服务器,您可以使用
NSImage
NSView
。您可以使用AppKit函数
NSApplicationLoad
与Windows服务器建立连接

main.m

#include <AppKit/AppKit.h>

int main(int argc, const char **argv) {
    if(argc != 3) {
        fprintf(stderr, "Usage: %s source_img dest_pdf\n", argv[0]);
        exit(1);
    }

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    BOOL success;
    NSString *imgPath, *pdfPath;
    NSImage *myImage;
    NSImageView *myView;
    NSRect vFrame;
    NSData *pdfData;

    imgPath = [NSString stringWithUTF8String:argv[1]];
    pdfPath = [NSString stringWithUTF8String:argv[2]];

    /* Calling NSApplicationLoad will give a Carbon application a connection
    to the window server and enable the use of NSImage, NSView, etc. */
    success = NSApplicationLoad();
    if(!success) {
        fprintf(stderr, "Failed to make a connection to the window server\n");
        exit(1);
    }

    /* Create image */
    myImage = [[NSImage alloc] initWithContentsOfFile:imgPath];
    if(!myImage) {
        fprintf(stderr, "Failed to create image from path %s\n", argv[1]);
        exit(1);
    }

    /* Create view with that size */
    vFrame = NSZeroRect;
    vFrame.size = [myImage size];
    myView = [[NSImageView alloc] initWithFrame:vFrame];

    [myView setImage:myImage];
    [myImage release];

    /* Generate data */
    pdfData = [myView dataWithPDFInsideRect:vFrame];
    [pdfData retain];
    [myView release];

    /* Write data to file */
    success = [pdfData writeToFile:pdfPath options:0 error:NULL];
    [pdfData release];
    if(!success) {
        fprintf(stderr, "Failed to write PDF data to path %s\n", argv[2]);
        exit(1);
    }

    [pool release];
    return 0;
}
编译时,您可以这样使用它:

./a.out myImage.png outFile.pdf

你可以很容易地使用CopeGeave/Syror来做这件事,如果你能超越你的应用程序中的“公正”基础。你会意识到<代码> NSImage < /代码>,更重要的是,在AppKi.Frror中定义了<代码> NSimaGeReP < /C>和朋友。那么AppKit真的不可能吗?
./a.out myImage.png outFile.pdf