我花了好几天的时间来寻找将带附件的属性字符串放入NSPasteboard的解决方案。
我可以读取带有附件的RTF文件,修改其文本和属性,然后将其粘贴在NSPasteboard上,用于其他应用程序(例如Mail.app),这很好。但我想做的是在文本中的某个点添加一个图像。我可以将文本作为属性字符串来实现这一点,但如果我尝试插入一个图像(作为属性字符串的附件),则图像永远不会到达(尽管其他图像会到达)。
RTFD似乎有各种各样的风格,我认为我需要的是序列化。我尝试过许多NSPasteboard声明类型的变体,甚至使用了FileWrappers,但肯定缺少一些重要的东西。无论我做什么,依恋似乎永远不会到来。
奇怪的是,如果我读到一个有图像附件的RTF文件,对其进行修改并将其粘贴到粘贴板中,那么这些原始附件就可以正常工作-如果我试图添加新的附件,它们就做不到了。例如,读一个RTF文件,处理它,加载粘贴板,并将结果粘贴到邮件中。所有原始文本和图像,以及任何新修改或添加的文本和属性都会显示出来,但只是缺少一个附加的图像。
下面是一些示例代码:
用一些文本制作一个属性字符串,然后添加一个附加的图像,然后再添加一点文本,在textView中显示它(所有这些都有效),然后加载粘贴板并粘贴到textEdit或Mail。。。附加的图像不在那里,尽管其余的是:
// 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: nn"] 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:@"nn 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.