当我打电话给CGFontCreateWithDataProvider
当处于飞行模式时,我的应用程序冻结了。不在飞行模式下,它工作正常。字体存储在设备上,不应进行网络访问。
调用该方法加载本地字体文件时:
[self registerIconFontWithURL:[[NSBundle mainBundle] URLForResource:@"FontAwesome" withExtension:@"otf"]];
的价值url
在下面的方法中是:文件:///var/mobile/Applications/48D3DA14-235B-4450-A081-7A6BA6116667/Blah.app/FontAwesome.otf
+ (void)registerIconFontWithURL:(NSURL *)url
{
NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[url path]], @"Font file doesn't exist");
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
//******Freezes after the next line******
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
//******FROZEN******
CGDataProviderRelease(fontDataProvider);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(newFont, &error);
CGFontRelease(newFont);
}
iOS 7.1.x、iPhone 5。
我不知道为什么会发生这种情况,也无法在那里找到任何帮助。有谁知道为什么会发生这种情况?
提前致谢!
经过更多搜索后我找到了答案。
根据以下错误报告:https://alpha.app.net/jaredsinclair/post/18555292
如果没有网络连接,CGFontCreateWithDataProvider() 会挂起
在信号量陷阱中if它在应用程序 DidFinishLaunchingWithOptions 期间调用。在下一个运行循环中调用它不会挂起。
正如同一篇文章中所述,调用
[UIFont familyNames]
before CGFontCreateWithDataProvider()
解决问题。我的代码现在看起来像这样并且按预期工作:
+ (void)registerIconFontWithURL:(NSURL *)url
{
NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[url path]], @"Font file doesn't exist");
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
//THE NEXT LINE IS RELEVANT PART
[UIFont familyNames];
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(newFont, &error);
CGFontRelease(newFont);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)