2012-03-16 125 views
2

我花了好幾天的時間搜索解決方案,將帶附件的屬性字符串放到NSPasteboard上。屬性字符串失去粘貼板粘貼中的附件

我可以讀取帶有附件的RTFD文件,修改其文本和屬性,然後將其粘貼到NSPasteboard上以用於其他應用程序(例如Mail.app),並且工作正常。但是我想要做的是在文本的某個位置添加圖片。我可以用文本作爲屬性字符串來做到這一點,但如果我嘗試插入圖像(作爲屬性字符串的附件),圖像永遠不會到達(儘管其餘部分)。

似乎RTFD有各種口味,而我認爲我需要的口味是序列化的。我已經嘗試了許多NSPasteboard聲明類型的變體,甚至使用FileWrappers,但必須丟失重要的東西。無論我做什麼,這個依戀似乎都不會到達。

奇怪的是,如果我閱讀一個帶有圖像附件的RTFD文件,將其修改並粘貼到一個pasteBoard中,那些原始附件就可以正常工作 - 如果我嘗試添加新附件,它們不會生成附件。一個例子是讀取RTFD文件,對其進行處理,加載粘貼板,並將結果粘貼到郵件中。所有原始文本和圖像以及任何新的修改或添加的文本和屬性都會顯示出來,但附加的圖像完全缺失。

下面是一些示例代碼:

做一個屬性串用一些文字,然後添加一個附加的圖像,然後有點更多的文本,在TextView中顯示它(所有工作),然後裝載紙板和粘貼文本編輯或郵件...附加的圖像不存在,但其餘的是:

// get the image 
NSImage *myImage = [[NSImage alloc] initWithData: [window dataWithPDFInsideRect:[theImage frame]]]; 

// set the image as an attachment 
NSTextAttachment  *myAttachment = [[NSTextAttachment alloc] init]; 
NSTextAttachmentCell *myAttachmentCell = [[NSTextAttachmentCell alloc] initImageCell:myImage]; 
[myAttachment setAttachmentCell:myAttachmentCell]; 

// put image inside attributed string 
NSAttributedString *myImageString = [NSAttributedString attributedStringWithAttachment:myAttachment] ; 


// make an attributes dictionary (simply makes text blue) as an example 
NSDictionary *myAttributesDict = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSColor blueColor], NSForegroundColorAttributeName, 
           nil]; 

// and add some beginning text 
NSMutableAttributedString *theCombinedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Here's an image we just grabbed: \n\n"] attributes:myAttributesDict]; 


// now append our attached image 
[theCombinedString appendAttributedString:myImageString]; 


// and add some following text as an example 
NSMutableAttributedString *endString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n\n How about that!\n"] attributes:myAttributesDict]; 


// and stick it all together 
[theCombinedString appendAttributedString: endString]; 


// now display it in a textView to make sure we have something 
[[junkTextView textStorage] appendAttributedString: theCombinedString]; 



/// --- works just fine to here --- /// 



// the following loads the pastboard, including the added text, but for some reason, leaves out the above attachment 

NSPasteboard *thePboard = [NSPasteboard generalPasteboard]; 
[thePboard clearContents]; 

NSAttributedString *theContents = [[NSAttributedString alloc] theCombinedString ]; 

[thePboard writeObjects:[NSArray arrayWithObject:theContents]]; 


// pasting into mail or textEdit shows the above before and after text, but not the image. 

任何想法?

我試過使用NSData,NSFileWrapper序列化,設置各種紙板類型等等。到目前爲止,似乎沒有任何工作。如果我將圖像作爲TIFF數據加載,它會很好地粘貼,但我需要它作爲屬性字符串,以便從已有附件的文件中插入較大的字符串。

這是我第一次在這裏發佈,所以請原諒任何格式錯誤 - 我會學習,並非常感謝任何指針或幫助,即使它是RTFM,我已經做了,但可能誤解了。

回答

7

終於找到了解決方案,畢竟它是一個包裝工具。以下是對任何感興趣的人的代碼,適用於從文件讀取的圖像或從您的應用中抓取的圖像:

// make a file wrapper 
NSFileWrapper* wrapper =[[NSFileWrapper alloc] initRegularFileWithContents:[theImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1]]; 

// -must- have this. used to save your pasted file in some apps 
[wrapper setPreferredFilename:@"yourImage.tiff"]; 
// 
NSAttributedString* imageString = [NSAttributedString attributedStringWithAttachment:[[NSTextAttachment alloc] initWithFileWrapper:wrapper]]; 

// then load pasteboard and paste wherever you wish. You can get fancier using private 
// pasteboards and custom data types, of course. This is just a simple example.