2011-04-22 11 views
1

我是Cocoa和Objective-C的初學者。如何使用Cocoa在PDF中生成網格?

我想製作一個Cocoa應用程序,它將生成一個盒子(用於練習中國書法)的網格以PDF形式導出,類似於這個在線生成器:http://incompetech.com/graphpaper/chinesequarter/

我該如何生成網格?我試圖用CustomView來使用Quartz,但並沒有設法得到很多。此外,一旦在CustomView中繪製了網格,將其「打印」爲PDF的方法是什麼?

感謝您的幫助。

+0

哦,對不起;我忘了提及我在談論一個Cocoa桌面應用程序,而不是Cocoa touch。 – 2011-04-22 21:23:18

+0

可可標籤用於Mac OS X上的Cocoa,因此無論如何都是隱含的。 – 2011-04-22 21:30:50

回答

0

我應該如何生成的網格?

執行一個custom view來繪製它。

我試圖使用Quartz與CustomView,...

這是一種方式;另一個是AppKit drawing。雖然他們大部分都非常相似, AppKit直接基於PostScript,而Quartz間接基於PostScript。

...但沒有設法得到很遠。

你應該問一個關於你的問題的更具體的問題。

此外,一旦在CustomView中繪製網格,「打印」到PDF的方法是什麼?

發送它a dataWithPDFInsideRect: message,通過其bounds

請注意,沒有「一旦在自定義視圖中繪製網格」。儘管可能有一些內部緩存,但從概念上來說,一個視圖並不是一次繪製並保留下來;它在需要的時候繪製,每次需要時繪製到需要的地方。當窗口需要重新繪製時,Cocoa會告訴任何在髒區域中的視圖(重新)繪製,並且它們將最終繪製到屏幕上。當您詢問PDF數據時,這也會告訴視圖繪製,並且將繪製到記錄PDF數據的上下文中。這使得視圖既可以是懶惰的(只在需要時才繪製),也可以在不同的上下文中進行不同的繪製(例如,打印時)。

0

哎呀,你是在問Cocoa,這是Cocoa Touch,但我會把它留在這裏,因爲它可能有些用處(至少對於後來發現它的人來說)。

您可以在視圖中繪製事物,然後將其中的內容放入pdf中。

這段代碼將採用UIView(在此稱爲sheetView)中繪製的內容,將其放入pdf中,然後將其作爲附件放在電子郵件中(以便您現在可以看到它)。您需要在頭中引用協議MFMailComposeViewControllerDelegate。

if ([MFMailComposeViewController canSendMail]) { 
    //set up PDF rendering context 
    NSMutableData *pdfData = [NSMutableData data]; 
    UIGraphicsBeginPDFContextToData(pdfData, sheetView.bounds, nil); 
    UIGraphicsBeginPDFPage(); 

    //tell our view to draw (would normally use setNeedsDisplay, but need drawn now). 
    [sheetView drawRect:sheetView.bounds]; 

    //remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    //send PDF data in mail message as an attachment 
    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease]; 
    mailComposer.mailComposeDelegate = self;If 
    [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SheetView.pdf"]; 
    [self presentModalViewController:mailComposer animated:YES]; 
} 
else { 
    if (WARNINGS) NSLog(@"Device is unable to send email in its current state."); 
} 

您還需要這種方法...

#pragma mark - 
#pragma mark MFMailComposeViewControllerDelegate protocol method 
//also need to implement the following method, so that the email composer can let 
//us know that the user has clicked either Send or Cancel in the window. 
//It's our duty to end the modal session here. 
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    [self dismissModalViewControllerAnimated:YES]; 
} 
+0

請注意,這是[可可](http://stackoverflow.com/tags/cocoa)問題,您的答案僅適用於[cocoa-touch](http://stackoverflow.com/tags/cocoa-touch) 。 – 2011-04-22 21:20:12

+0

感謝您的代碼。我將如何改變這個在非Cocoa-touch程序中運行? – 2011-04-22 21:31:47

+0

對不起,我不是一個Mac程序員,但希望有人會很快...... :) – DenverCoder9 2011-04-22 21:34:17