归属字符串在粘贴板粘贴中丢失附件

dal*_*ale 3 attachment nspasteboard

我花了很多天时间寻找一个解决方案,将带有附件的属性字符串放到NSPasteboard上.

我可以读取带有附件的RTFD文件,修改其文本和属性,然后将其粘贴在NSPasteboard上以便在其他应用程序(例如Mail.app)中使用,并且工作正常.但我想做的还是在文本的某个位置添加图像.我可以将文本作为属性字符串来实现,但如果我尝试插入图像(作为属性字符串的附件),图像永远不会到达(尽管其余部分也是如此).

似乎RTFD有各种各样的风格,我认为我需要的是序列化的.我已经尝试了NSPasteboard声明类型的许多变体,即使使用FileWrappers,但必须缺少重要的东西.无论我做什么,附件似乎永远不会到来.

奇怪的是,如果我读取一个包含图像附件的RTFD文件,修改它并将其粘贴在pasteBoard中,那些原始附件就可以正常工作 - 如果我尝试添加新附件,它们就不会成功.一个例子是读取RTFD文件,处理它,加载粘贴板,并将结果粘贴到邮件中.所有原始文本和图像,以及任何新修改或添加的文本和属性都会显示,但附加的图像根本就丢失了.

这是一些示例代码:

使用一些文本创建一个属性字符串,然后添加一个附加图像,然后添加一些文本,在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: \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.  
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

我尝试过使用NSData,NSFileWrapper序列化,设置各种粘贴板类型等等.到目前为止,似乎没有任何效果.如果我将图像加载为TIFF数据,它会很好地粘贴,但我需要它作为属性字符串从一个已经有附件的文件插入一个更大的字符串.

这是我在这里的第一篇帖子,所以请原谅任何格式错误 - 我会学习,非常感谢任何指针或帮助,即使它是RTFM,我已经做过但可能有误解.

dal*_*ale 7

终于找到了解决方案,毕竟它是一个Wrapper.以下是感兴趣的人的代码,使用从文件中读取的图像或从应用程序中获取的图像:

// 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.
Run Code Online (Sandbox Code Playgroud)