【发布时间】:2011-01-09 09:27:04
【问题描述】:
我有一个函数,它接受一些位图数据并从中返回一个 UIImage *。它看起来像这样:
UIImage * makeAnImage()
{
unsigned char * pixels = malloc(...);
// ...
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, pixelBufferSize, NULL);
CGImageRef imageRef = CGImageCreate(..., provider, ...);
UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];
return [image autorelease];
}
谁能在这里准确地解释谁拥有什么内存?我想正确清理,但我不确定如何安全地清理。文档对这些内容很模糊。如果我在创建 UIImage 后在此函数末尾 free 像素,然后使用 UIImage,我会崩溃。如果我在创建 UIImage 后释放提供程序或 imageRef,我不会看到崩溃,但它们显然是一路传递像素,所以我对释放这些中间状态感到不安。
(根据 CF 文档,我知道我应该对后者调用 release,因为它们来自 Create 函数,但我可以在使用 UIImage 之前这样做吗?)大概我可以使用提供者的 dealloc 回调来清理像素缓冲区,但还有什么?
谢谢!
【问题讨论】:
标签: iphone uiimage core-graphics quartz-2d core-foundation