转换 NSAttributedString 中的 html 数据的时间太长。有任何想法吗?
Too long time for converting html data in a NSAttributedString. Any ideas?
我正在为 macOS 开发 Objective-C 应用程序。在某些情况下,我需要使用以下代码将 html 数据转换为富文本格式:
NSDictionary *htmlAttrs = nil;
NSMutableAttributedString *rtfAttributedString = [[NSMutableAttributedString alloc] initWithHTML:data documentAttributes:&htmlAttrs];
问题是,有时,转换 html 一封简单电子邮件的正文,最多可能需要 40 秒,因此这个解决方案对我来说是不可接受的。我知道在堆栈溢出上还有其他类似的问题,但我想让你注意到一件奇怪的事情:在转换过程结束时,日志中会出现这样一行:
Task <A8B047AC-DABA-4259-AAF7-E2C23C84A2F1>.<0> HTTP load failed (error code: -999 [1:89])
这让我觉得 NSAttributedString 可能正在尝试解析一些可能不存在的 http 链接,所以它有点等待超时(40 秒超出了正常的转换持续时间,所以这可能是真的) .如果这是真的,那么可能有某种方式告诉 NSAttributedString,通过其他采用更多选项的类似方法,例如不解析链接。当然,我可能完全错了。任何帮助是极大的赞赏。谢谢
我还没有机会尝试这个,但是如果你使用初始化程序的 initWithHTML:options:documentAttributes:
版本,你可以使用 Timeout
选项。
NSDictionary *htmlAttrs = nil;
NSDictionary *options = @{ NSTimeoutDocumentOption: @(0.5) };
NSMutableAttributedString *rtfAttributedString = [[NSMutableAttributedString alloc] initWithHTML:htmlData options:options documentAttributes:&htmlAttrs];
该值应该是以秒为单位的超时。
我正在为 macOS 开发 Objective-C 应用程序。在某些情况下,我需要使用以下代码将 html 数据转换为富文本格式:
NSDictionary *htmlAttrs = nil;
NSMutableAttributedString *rtfAttributedString = [[NSMutableAttributedString alloc] initWithHTML:data documentAttributes:&htmlAttrs];
问题是,有时,转换 html 一封简单电子邮件的正文,最多可能需要 40 秒,因此这个解决方案对我来说是不可接受的。我知道在堆栈溢出上还有其他类似的问题,但我想让你注意到一件奇怪的事情:在转换过程结束时,日志中会出现这样一行:
Task <A8B047AC-DABA-4259-AAF7-E2C23C84A2F1>.<0> HTTP load failed (error code: -999 [1:89])
这让我觉得 NSAttributedString 可能正在尝试解析一些可能不存在的 http 链接,所以它有点等待超时(40 秒超出了正常的转换持续时间,所以这可能是真的) .如果这是真的,那么可能有某种方式告诉 NSAttributedString,通过其他采用更多选项的类似方法,例如不解析链接。当然,我可能完全错了。任何帮助是极大的赞赏。谢谢
我还没有机会尝试这个,但是如果你使用初始化程序的 initWithHTML:options:documentAttributes:
版本,你可以使用 Timeout
选项。
NSDictionary *htmlAttrs = nil;
NSDictionary *options = @{ NSTimeoutDocumentOption: @(0.5) };
NSMutableAttributedString *rtfAttributedString = [[NSMutableAttributedString alloc] initWithHTML:htmlData options:options documentAttributes:&htmlAttrs];
该值应该是以秒为单位的超时。