2012-01-04 4 views
0

NSImage를 Foundation에서 PDF 파일로 저장하는 방법은 무엇입니까? 이것은 AppKit (따라서 NSView)이 사용되지 않도록 GUI 응용 프로그램이 아닙니다.NSImage를 PDF 파일로 출력

편집 : 글쎄, 지금은 어리 석다. NSImage는 AppKit의 일부이므로 사용 중입니다. 그러나 내 질문은 여전히 ​​의미합니다 : 당신은 PDF로 NSImage를 어떻게 저장합니까?

+0

: 당신이 그것을 컴파일 할 때

gcc -framework Foundation -framework AppKit main.m 

이처럼 사용할 수 있습니다 당신은 CoreGraphics/Quartz를 사용하여 "그냥"넘어갈 수 있다면 아주 쉽게 할 수 있습니다. 앱 기반. –

+0

당신은 정교 할 수 있습니까? – Anonymous

+1

'NSImage'와 더 중요한 것은'NSImageRep'과 친구들 **이 AppKit.framework에 정의되어 있다는 것을 알고 계십니까? AppKit은 정말 궁금한가요? – NSGod

답변

2

창 서버에 연결하면 NSImageNSView을 사용할 수 있습니다.AppKit 함수를 사용하여 창 서버에 연결할 수 있습니다.

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