我有一些非常简单的RTF文件输出。当我生成这个文档时,用户可以通过电子邮件发送它。所有这些都很好用。文档看起来很好。一旦我有了NSAttributedString,我就会制作一个NSData块,并将其写入文件中,就像这样:
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在调试器中看起来很好,但最终输出中没有图像。
提前感谢任何对此的帮助。
{{\NeXTGraphic Attachment.png \width400 \height400}¨}
,以及一堆数据在结尾处,在十六进制转储中,我看到标记:89 50 4E 47 0D 0A 1A 0A
。也许这是值得研究的东西。我还看到你尝试了Apple Dev论坛,也许可以报告一个错误。 - Larme