gpt4 book ai didi

objective-c - 垃圾收集器和核心基础

转载 作者:行者123 更新时间:2023-12-03 17:30:53 25 4
gpt4 key购买 nike

我编写了一个将图像加载到 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 中不起作用。
谢谢,请原谅我的英语。

最佳答案

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

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

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.

关于objective-c - 垃圾收集器和核心基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com