ios - CGFontCreateWithDataProvider 在飞行模式下挂起

标签 ios objective-c fonts freeze

当我调用 CGFontCreateWithDataProvider 在飞行模式下时,我的应用程序卡住了。不在飞行模式下,它工作正常。字体存储在设备上,不应进行网络访问。

调用此方法加载本地字体文件时:

[self registerIconFontWithURL:[[NSBundle mainBundle] URLForResource:@"FontAwesome" withExtension:@"otf"]];

以下方法中url的值为: file:///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

If there’s no network connection, CGFontCreateWithDataProvider() hangs in a semaphore trap if it’s called during appDidFinishLaunchingWithOptions. Calling it in the next run loop works without hanging.

如同一篇文章所述,调用

[UIFont familyNames]

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);
}

关于ios - CGFontCreateWithDataProvider 在飞行模式下挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900979/

相关文章:

ios - UICollectionView 项目按其部分位置偏移

ios - "No valid iOS code signing keys found in keychain"

ios - iCloud 正在备份我的应用程序的 68 字节(一个 plist 文件),我是否需要设置一个标志以将其从 icloud 备份中排除?

ios - 根据文本内容的高度设置单元格的高度

fonts - 如何使用音乐字体 Bravura Text?

ios - 在SKPSMTPMessage中附加视频

ios - Uiimageview 动画没有显示

objective-c - iOS - 将文本 append 到应用程序包中的文件

html - 同一段落中的多种样式

ios - Obj-C 用于横向的替代 Storyboard