-1

我正在加载 HTML 字符串UIWebView,文本内容会在几秒钟内加载,但字符串中有很多图像HTML,导致此图像UIWebView冻结,加载所有图像后,我的屏幕可以免费使用。

对此有何建议?

4

3 回答 3

1

在调度线程中加载 webview,试试这个:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.webView loadHTMLString:@"htmlstring" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
});
于 2016-06-22T11:15:57.740 回答
0

我认为这很冻结,因为您正在主线程中从网络加载图像,这也负责重绘您的 UI。通过在线程中加载大图像,您的 UI 也会冻结。

我建议将执行图像加载的代码移动到单独的线程中。(您可以使用@Andey 的答案)

于 2016-06-22T11:20:52.313 回答
0

就像@Andey 所说,只要您正在做的事情需要一些时间并且您不希望您的视图冻结,您就应该调度主队列。

尝试第 9 讲: https ://itunes.apple.com/en/course/developing-ios-8-apps-swift/id961180099


更新斯威夫特版):

虽然我不知道如何在Obj-C中编写它,但它在Swift中看起来像这样,稍后您可以将其翻译回Obj-c

dispatch_async(notTheMainQueue) {
    // Some time consuming stuff you're doing (downloading data, calculating..)
    ...
    dispatch_async(dispatch_get_main_queue) {
        // Set your view here, which dispatches *back* to the main queue and will not block your UI
        ...
    }
}
于 2016-06-22T11:26:31.640 回答