垃圾收集器和核心基础
我编写了一个将图像加载到 CALayer 的方法。这是代码:
- (CGImageRef)loadImage:(NSString*)path {
// Get data image
CGImageRef image = NULL;
NSData *data = [NSData dataWithContentsOfFile:path];
CFDataRef imgData = (CFDataRef)data;
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData (imgData);
// Get CGImage from CFDataRef
image = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
// If the image isn't a JPG Image, would be PNG file
if (!image)
image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
return image;
}
我在我的 CALayer 中使用这个方法:
NSString *pathString = // my image path;
aLayer = [CALayer layer];
aLayer.contents = [self loadImage:pathString];
它有效。我最终确定了我的观点(使用垃圾收集器),但我的应用程序存在泄漏。我应该释放 CFDataRef imgData 吗?我读到垃圾收集器在 Core Foundation 中不起作用。
谢谢,请原谅我的英语。
I wrote a method for load Image into CALayer. This is the code:
- (CGImageRef)loadImage:(NSString*)path {
// Get data image
CGImageRef image = NULL;
NSData *data = [NSData dataWithContentsOfFile:path];
CFDataRef imgData = (CFDataRef)data;
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData (imgData);
// Get CGImage from CFDataRef
image = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
// If the image isn't a JPG Image, would be PNG file
if (!image)
image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
return image;
}
I use this method in my CALayer:
NSString *pathString = // my image path;
aLayer = [CALayer layer];
aLayer.contents = [self loadImage:pathString];
It's work. I finalize my view (using garbage collector) but my application has leaks. Should I release CFDataRef imgData? I read that garbage collector does not work in Core Foundation.
Thanks and excuse my english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您负责通过调用 CGImageRelease 来释放该对象。
请参阅有关垃圾收集的文档:
http:// /developer.apple.com/library/mac/#documentation/cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html
You are responsible for releasing this object by calling CGImageRelease.
See the documentation on garbage collection:
http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html
垃圾收集器确实适用于 CoreFoundation 类型。
请参阅 CFMakeCollectable(摘自 CF 文档):
the garbage collector does work with CoreFoundation types.
see CFMakeCollectable (excerpt from CF docs):