gpt4 book ai didi

ios - NSAttributedString 初始化数据 :options:documentAttributes:error: is executing very slowly

转载 作者:可可西里 更新时间:2023-11-01 17:08:11 24 4
gpt4 key购买 nike

我正在试用新的 iOS 7 API,执行以下代码需要一分钟多的时间!

NSLog(@"start encoding");
NSString *htmlBody = @"<html><body>I am <b>bold</b> and <i>italic</i>";
NSStringEncoding encoding = NSUnicodeStringEncoding;
NSData *data = [htmlBody dataUsingEncoding:encoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(encoding)};

NSAttributedString *body = [[NSAttributedString alloc] initWithData:data
options:options
documentAttributes:nil
error:nil];

NSLog(@"end encoding%@", body);

这是怎么回事?

最佳答案

在做了一些调查之后,我意识到我正在调用这个方法作为 webview didFinishLoading 回调的一部分。我相信这会导致某种形式的实时锁定或等待 WebKit 的线程。解决方案是让该 webview 方法从 dispatch_aync block 内部调用其委托(delegate)(或其他代码):

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (self.successBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
//this success block eventually calls my NSAttributedString code above
self.successBlock();
});
}
}

关于ios - NSAttributedString 初始化数据 :options:documentAttributes:error: is executing very slowly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com