垃圾收集器和核心基础

发布于 2024-09-26 04:13:17 字数 1031 浏览 1 评论 0原文

我编写了一个将图像加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-10-03 04:13:17

您负责通过调用 CGImageRelease 来释放该对象。

请参阅有关垃圾收集的文档:

http:// /developer.apple.com/library/mac/#documentation/cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html

因此,默认情况下,在垃圾收集环境中,您管理 Core Foundation 对象的方式与在引用计数环境中完全相同(如 Core Foundation 内存管理编程指南 >“所有权策略”中所述)。如果创建或复制 Core Foundation 对象,则必须在完成后释放它。如果您想保留 Core Foundation 对象,则必须保留它,并在使用完毕后再次释放它。

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

By default, therefore, in a garbage-collected environment you manage Core Foundation objects exactly as you would in a reference-counted environment (as described in Memory Management Programming Guide for Core Foundation > “Ownership Policy”). If you create or copy a Core Foundation object, you must subsequently release it when you’re finished with it. If you want to keep hold of a Core Foundation object, you must retain it and again subsequently release it when you’re finished with it.

断舍离 2024-10-03 04:13:17

垃圾收集器确实适用于 CoreFoundation 类型。

请参阅 CFMakeCollectable(摘自 CF 文档):

CFTypeRef CFMakeCollectable(CFTypeRef)
比照);

创建新分配的核心
符合垃圾条件的基础对象
收藏。

参数 cf 要创建的 CFType 对象
可收藏的。该值一定不能是
无效的。返回值cf

讨论有关更多详细信息,请参阅
垃圾收集编程指南。

the garbage collector does work with CoreFoundation types.

see CFMakeCollectable (excerpt from CF docs):

CFTypeRef CFMakeCollectable(CFTypeRef
cf);

Makes a newly-allocated Core
Foundation object eligible for garbage
collection.

Parameters cf A CFType object to make
collectable. This value must not be
NULL. Return Value cf.

Discussion For more details, see
Garbage Collection Programming Guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文