【发布时间】:2012-03-17 16:15:27
【问题描述】:
在处理图形库时如何处理内存管理,尤其是一些CoreText 对象时,我有点困惑。例如,我想为CTFontRef 创建一个属性,但我完全不确定我应该如何声明它。
1) 我应该把它当作一个原始的并做@property(nonatomic)吗?还是我根本不应该将其声明为财产?
2) 我假设我必须在处理完 CFRelease(myFont) 后处理它?如何正确释放它
3) 在下面的方法中,我的处理方式是否正确? CTFontRef 被返回但不是自动释放的。我需要担心这个吗?
- (CTFontRef) loadCustomFontWithName:(NSString *)fontName ofType:(NSString *)type attributes:(NSDictionary *)attributes
{
NSString *fontPath = [[NSBundle mainBundle] pathForResource:fontName ofType:type];
NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath];
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
[data release];
CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
CGDataProviderRelease(fontProvider);
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef font = CTFontCreateWithGraphicsFont(cgFont, 0, NULL, fontDescriptor);
CFRelease(fontDescriptor);
CGFontRelease(cgFont);
return font;
}
总体而言,对于如何考虑从 CoreGraphics 或 CoreText 等较低级别的库管理内存有点困惑,如果有人能告诉我一个考虑它的好方法,我将不胜感激。
【问题讨论】:
标签: iphone ios cocoa-touch core-graphics core-text