参考:
https://www.jianshu.com/p/5c061b15d2e1
https://www.jianshu.com/p/a02be724b7ec
这里5M左右的字体文件 会导致内存增长5M多多
虽然逻辑正确 但增加内存更多的使用 毫无疑问是个bug
优化后 有2个需要注意点
CGDataProviderCreateWithCFData和CGDataProviderCreateWithURL
CTFontManagerRegisterGraphicsFont和CTFontManagerRegisterFontsForURL
使用data比使用url增加了step 中间变量
@autoreleasepool
{
if(fontPath.length <= 0)
{
return;
}
NSURL *fontUrl = [NSURL fileURLWithPath:fontPath];
if(!fontUrl)
{
return;
}
CGDataProviderRef provider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(font == nil)
{
CFRelease(provider);
return;
}
CFErrorRef error;
if(!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontUrl, kCTFontManagerScopeProcess, &error))
{
CFStringRef errorDescription = CFErrorCopyDescription(error);
CFRelease(errorDescription);
}
NSString *fontName = (__bridge NSString *)CGFontCopyPostScriptName(font);
CGFontRelease(font);
CFRelease(provider);
if([UIFont fontWithName:fontName size:12])
{
return;
}
else
{
return;
}
}