属性字符串失去粘贴板粘贴中的附件
我花了好几天的时间搜索解决方案,将带附件的属性字符串放到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,我已经做了,但可能误解了。
终于找到了解决方案,毕竟它是一个包装工具。以下是对任何感兴趣的人的代码,适用于从文件读取的图像或从您的应用中抓取的图像:
// 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.