gpt4 book ai didi

ios - Core Text 将所有系统字体加载到驻留内存中

转载 作者:行者123 更新时间:2023-11-28 20:40:39 27 4
gpt4 key购买 nike

每当我使用 CTFontCreateWithFontDescriptor() 时(下面的代码片段引用自 article)

我发现所有字体都已映射,而且似乎浪费了大约 20MB。

dispatch_queue_t queue = dispatch_queue_create("font.worker", NULL);
dispatch_async(queue, ^(void) {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:@"Helvetica" forKey:(id)kCTFontFamilyNameAttribute];
[attributes setObject:[NSNumber numberWithFloat:36.0f] forKey:(id)kCTFontSizeAttribute];
CTFontDescriptorRef fontDesc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef matchingFont = CTFontCreateWithFontDescriptor(fontDesc, 36.0f, NULL);
CFRelease(matchingFont);
CFRelease(fontDesc);
});
dispatch_release(queue);

我听说它在 iOS 5 中已修复,所以问题是:

是否可以在 Core Text 中使用自定义字体,但只加载必要的字体?

enter image description here

最佳答案

在 iOS 5 之前,CoreText 会在您第一次搜索字体时始终加载整个字体映射表。那是使用字体描述符发生的。如果您直接通过名称实例化字体,则不会发生这种情况。

在 GitHub 上的 DTCoreText 中 https://github.com/Cocoanetics/DTCoreText我正在通过将自己的粗体和斜体与字体名称匹配然后从中创建字体来解决这个问题。

解决方法是在后台队列上实例化映射表负载。请参阅:http://www.cocoanetics.com/2011/04/coretext-loading-performance/从 iOS 5 开始,不再需要此解决方法。我打开了雷达,Apple 修复了它。

PS:映射文件不使用RAM,它只是映射到地址空间,通过指针访问。加载导致了另一个问题,如果在主线程上完成,它会在设备上花费 1 秒导致暂停。

关于ios - Core Text 将所有系统字体加载到驻留内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711790/

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