我发现在 iOS5 中使用 CTFramesetterCreateWithAttributedString 创建带有中文字符串的 framesetter 非常慢,但在 iOS4 中,它很快。
我已经运行了这样的测试: (chinese.txt 包含 77571 个中文字符和 english.txt 包含 233727 个字符)
NSString *englishCtn = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [self documentDirectory], @"english.txt"] encoding:NSUTF8StringEncoding error:nil];
NSAttributedString *englishCtnA = [[NSAttributedString alloc] initWithString:englishCtn];
NSString *chineseCtn = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [self documentDirectory], @"chinese.txt"] encoding:NSUTF8StringEncoding error:nil];
NSAttributedString *chineseCtnA = [[NSAttributedString alloc] initWithString:chineseCtn];
double start = [[NSDate date] timeIntervalSince1970];
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)englishCtnA);
NSLog(@"english length:%d time %f", englishCtn.length, ([[NSDate date] timeIntervalSince1970] - start) * 1000);
start = [[NSDate date] timeIntervalSince1970];
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)chineseCtnA);
NSLog(@"chinese length:%d time %f", chineseCtnA.length, ([[NSDate date] timeIntervalSince1970] - start) * 1000);
在 iOS 5 中结果是:
中文长度:77571 时间:12140.347004
英文长度:233727 时间:75.886011
在 iOS 4 中结果是:
中文长度:77571 时间:53.114176
英文长度:233727 时间:55.696011
我用xCode工具time profiler看看会发生什么,我发现函数TRun::GetNextLigatureCandidateCharRange(long)占用了大部分时间,我不知道如何优化,求助
最佳答案
运行时的 C++ 部分大部分(如果不是全部)添加到 iOS 5 中,这就是它失败的地方。对我来说,这表明您应该向 Apple 提交错误报告,或者在您的帐户上进行查询(如果还有剩余)。
关于iphone - 为什么在 iOS5 中用中文创建 framesetter 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10029736/