2012-01-04 12 views
0

Foundationを使用してNSImageをPDFファイルに保存するにはどうすればよいですか?これはGUIアプリケーションではないので、AppKit(したがってNSView)は使用されていません。NSImageをPDFファイルに出力する

編集:まあ、私は今や馬鹿だ。 NSImageはAppKitの一部であるため、使用中です。しかし、私の質問はまだ立っています:どのようにNSImageをPDFに保存しますか?

+0

:あなたはそれをコンパイルするとき

gcc -framework Foundation -framework AppKit main.m 

あなたはこのようにそれを使用することができますCoreGraphics/Quartzを使用すると、これを簡単に行うことができます。あなたのアプリで財団。 –

+0

詳しいことはできますか? – Anonymous

+1

あなたは 'NSImage'、さらに重要なのは' NSImageRep'とfriends **がAppKit.frameworkで**定義されていることに気づきますか? AppKitは本当に問題になりませんか? – NSGod

答えて

2

ウィンドウサーバーに接続すると、NSImageNSViewを使用できます。ウィンドウサーバーとの接続は、AppKit関数NSApplicationLoadを使用して行うことができます。

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; 
} 

財団とのAppKitがリンクフレームワークでこれをコンパイルします。

./a.out myImage.png outFile.pdf 
関連する問題