
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 attachmentNSMutableParagraphStyle *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
虽然您将能够获得包含文本和图像的NSData对象(根据其大小判断),但是通过dataFromrange:documentAttributes:@ {NSdocumentTypedocumentAttribute:NSRTFDTextdocumentType}错误:]您可能无法保存它以便它可以成功打开.至少 – 我无法做到这一点.
这可能是因为实际上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 app当用户点击它时如何显示附件的内容.实际上它也适用于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文件时遇到问题全部内容,希望文章能够帮你解决ios – 将带有图像的NSAttributedString保存到RTF文件时遇到问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)