ios - 可以异步加载 UILabel/UITextView 的 NSAttributesString(从 HTML 生成)中的图像(iOS 7+)吗?

标签 ios objective-c uilabel nsattributedstring

最近我正在尝试实现一个富 TextView 来显示主题内容或回复等内容(通常包括一些图像)。我在UITableView中尝试了DTCoreText,但似乎有很多问题。

我刚刚发现从iOS 7开始,UITextView/UILabel开始支持NSAttributedText,但问题是所有图像都是同步加载的,它们会阻塞主线程。有什么办法可以异步加载它们吗?

示例代码:
https://gist.github.com/romaonthego/6672863

最佳答案

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

在这一行中,您应该先下载数据,然后再将其设置为属性字符串。

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // Load Data Here.
    dispatch_async(dispatch_get_main_queue(), ^{
        // Create NSAttributedString and set data to it.
    });
});

关于ios - 可以异步加载 UILabel/UITextView 的 NSAttributesString(从 HTML 生成)中的图像(iOS 7+)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984783/

相关文章:

ios - 我没有得到正确的信号值来识别 iPhone 和蓝牙设备之间的距离

java - Mobile - Codename - 怎么比母语快?

ios - UILabel - 如何在 Swift 3 中的行之间添加空格

iphone - uitableview 的最后一行中的值发生变化

ios - 设备 token 获取 Nil

c# - xamarin ios uilabel 问题

ios - 这是什么代码监控? -> `stack_logging_type_dealloc|stack_logging_type_alloc`

iphone - 通用应用程序 - 仅更新 iPhone 版本

ios - (xcode) 我想长按一个按钮导致键盘弹出

ios - 可以限制 iOS 应用程序发布到的国家/地区吗?