ios - 将带有图像的 NSAttributedString 保存到 RTF 文件时遇到问题

标签 ios ios7 nsattributedstring nstextattachment

我有一些输出是一个非常简单的 RTF 文件。当我生成此文档时,用户可以通过电子邮件发送它。这一切都很好。文件看起来不错。获得 NSAttributedString 后,我创建一个 NSData block ,并将其写入文件,如下所示:

NSData* rtfData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:&error];

此文件可以通过电子邮件发送。当我检查电子邮件时,一切都很好。

现在,我的任务是在文档顶部添加一个 UIImage。太好了,所以我正在创建一个这样的属性字符串:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

UIImage* image = [UIImage imageNamed:@"logo"];
attachment.image = image;
attachment.bounds = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];

// sets the paragraph styling of the text attachment

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;

[paragraphStyle setAlignment:NSTextAlignmentCenter];            // centers image horizontally

[paragraphStyle setParagraphSpacing:10.0f];   // adds some padding between the image and the following section

[imageAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [imageAttrString length])];
[imageAttrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];

此时,在 Xcode 中,我可以对 imageAttrString 执行 QuickLook,它绘制得很好。

构建此字符串后,我将执行此操作:

[attrString appendAttributedString:imageAttrString];

然后添加我最初生成的所有其余属性文本。

当我现在查看文件时,没有图像。 QuickLook 在调试器中看起来不错,但在最终输出中没有图像。

在此先感谢您提供的任何帮助。

最佳答案

尽管 RTF 在 Windows 上支持嵌入式图像,但显然它在 OS X 上不支持。RTF 是由 Microsoft 开发的,他们在 1.5 版 (http://en.wikipedia.org/wiki/Rich_Text_Format#Version_changes) 中添加了嵌入式图像。我认为 Apple 采用了早期版本的格式,他们对文档中图像的解决方案是 RTFD。以下是 Apple 文档中关于 RTF 的内容:

Rich Text Format (RTF) is a text formatting language devised by Microsoft Corporation. You can represent character, paragraph, and document format attributes using plain text with interspersed RTF commands, groups, and escape sequences. RTF is widely used as a document interchange format to transfer documents with their formatting information across applications and computing platforms. Apple has extended RTF with custom commands, which are described in this chapter.

所以没有提到图像。最后证明RTF在mac上不支持图片,下载this RTF document - 它会在 Windows 写字板中显示照片,而不会在 OS X TextEdit 中显示。

正如 Larme 提到的那样 - 添加附件时您应该选择 RTFD 文件类型。来自维基百科:

Rich Text Format Directory, also known as RTFD (due to its extension .rtfd), or Rich Text Format with Attachments

尽管您将能够通过 dataFromRange:documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType} error:] 获取包含文本和图像的 NSData 对象(根据其大小判断),您可能将无法保存它以便它可以成功打开。至少 - 我无法做到这一点。

这可能是因为实际上 RTFD 不是一种文件格式——它是一种捆绑格式。要检查它,您可以在 Mac 上使用 TextEdit 创建一个新文档,向其中添加图像和文本并将其另存为文件。然后右键单击该文件并选择显示包内容,您会注意到该目录包含您的图像和 RTF 格式的文本。

但是您可以使用以下代码成功保存此文档:

NSFileWrapper *fileWrapper = [imageAttrString fileWrapperFromRange:NSMakeRange(0, [imageAttrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType} error:&error];
[fileWrapper writeToURL:yourFileURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:&error];

因为显然 NSFileWrapper 知道如何处理 RTFD 文档,而 NSData 不知道它包含什么。

然而,主要问题仍然存在——如何通过电子邮件发送?因为 RTFD 文档是目录而不是文件,所以我认为它不太适合通过电子邮件发送,但是您可以压缩它并使用扩展名 .rtfd.zip.这里的扩展很关键,因为它会告诉 Mail 应用程序如何在用户点击附件时显示附件内容。实际上它也可以在 Gmail 和 iOS 上的其他电子邮件应用程序中使用,因为它是知道如何显示 .rtfd.zip 的 UIWebView。这是关于它的技术说明:https://developer.apple.com/library/ios/qa/qa1630/_index.html#//apple_ref/doc/uid/DTS40008749

所以底线是 - 它可以完成,但 RTFD 文档将是电子邮件的附件,而不是电子邮件内容本身。如果您想将其作为电子邮件内容,您可能应该考虑将您的图像嵌入到 HTML 中并以 HTML 格式发送邮件。

关于ios - 将带有图像的 NSAttributedString 保存到 RTF 文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23370275/

相关文章:

ios - SwiftUI 仅更改当前 View 的导航标题颜色

ios - Xcode 更新后过渡停止工作

ios - 当我在 UITextField 中键入内容时更新 UINavigation 标题

ios7 - 栏按钮未显示在工具栏xcode 5上

ios - UISearchBar 在搜索结果中显示多个部分标题

iOS 属性文本删除/n 换行符

xcode - 返回一个 NSObject - 错误的执行

ios - 如何使字典 NSDictionary 对于读取、插入是线程安全的?

cocoa - TextView 的多个撤消管理器

ios - 当使用 lineHeightMultiple 控制行间行距时,如何在 UITableViewCell 中垂直对齐我的 UILabel?