将带有图像的NSAttributedString保存到RTF文件时出现问题 [英] trouble saving NSAttributedString, with image, to an RTF file

查看:306
本文介绍了将带有图像的NSAttributedString保存到RTF文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some output that is a very simple RTF file. When I generate this document, the user can email it. All this works fine. The document looks good. Once I have the NSAttributedString, I make an NSData block, and write it to a file, like this:

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

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

This file can be emailed. When I check the email all is good.

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

Now, I'm tasked with adding a UIImage at the top of the document. Great, so I'm creating an attributed string like this:

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,并且它绘制得很好。

At this point, in Xcode, I can do a QuickLook on imageAttrString, and it draws just fine.

一旦构建了这个字符串,我就是这样做的:

Once this string is built, I'm doing this:

[attrString appendAttributedString:imageAttrString];

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

And then adding in all the rest of the attributed text that I originally generated.

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

When I look at the file now, there is no image. QuickLook looks good in the debugger, but no image in the final output.

提前感谢您对此的任何帮助。

Thanks in advance for any help with this.

推荐答案

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

Although, RTF does support embedded images on Windows, apparently it doesn't on OS X. RTF was developed by Microsoft and they added embedded images in version 1.5 (http://en.wikipedia.org/wiki/Rich_Text_Format#Version_changes). I think that Apple took earlier version of the format and their solution to images in documents was RTFD. Here is what Apple documentation says about RTF:


RTF格式(RTF)是Microsoft Corporation设计的文本格式化语言。您可以使用带有散布的RTF命令,组和转义序列的纯文本来表示字符,段落和文档格式属性。 RTF广泛用作文档交换格式,用于跨应用程序和计算平台传输文档及其格式信息。 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上的图像,请下载此RTF文档 - 它将显示照片Windows写字板,不会在OS X TextEdit中显示。

So no images are mentioned. Finally to prove that RTF doesn't support images on mac, download this RTF document - it will show photo in Windows WordPad and won't show it in OS X TextEdit.

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

So as Larme mentioned - you should choose RTFD file type when adding attachments. From Wikipedia:


富文本格式目录,也称为RTFD(由于其扩展名为.rtfd),或带有附件的RTF格式

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

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

Although you will be able to get NSData object that contains both the text and the image (judging by its size), via dataFromRange:documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType} error:] you probably won't be able to save it so that it could be opened successfully. At least - I wasn't able to do that.

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

That's probably because actually RTFD is not a file format - it's a format of a bundle. To check it, you could use TextEdit on your mac to create a new document, add image and a text to it and save it as a file. Then right click on that file and choose Show Package Contents and you'll notice that the directory contains both your image and the text in RTF format.

但是您将能够使用以下代码成功保存此文档

However you will be able to save this document successfully with this code:

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不知道它包含什么。

Because apparently NSFileWrapper knows how to deal with RTFD documents while NSData has no clue of what it contains.

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

However the main problem still remains - how to send it in email? Because RTFD document is a directory not a file, I'd say it's not very well suited for sending by email, however you can zip it and send with an extension .rtfd.zip. The extension here is the crucial because it will tell Mail app how to display contents of the attachment when user taps on it. Actually it will work also in Gmail and probably other email apps on iOS because it's the UIWebView that knows how to display .rtfd.zip. Here is a technical note about it: https://developer.apple.com/library/ios/qa/qa1630/_index.html#//apple_ref/doc/uid/DTS40008749

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

So the bottom line is - it can be done but the RTFD document will be an attachment to the email not the email content itself. If you want to have it as an email content you should probably look into embedding your image into HTML and sending the mail as HTML.

这篇关于将带有图像的NSAttributedString保存到RTF文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆