我有一个函数,它接受一些位图数据并从中返回一个 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];
}
谁能在这里准确解释谁拥有什么记忆?我想正确清理,但我不确定如何安全地清理。文档对这些内容很模糊。如果我free
在创建 UIImage 后在此函数的末尾像素,然后使用 UIImage,我会崩溃。如果我在创建 UIImage 后释放提供程序或 imageRef,我看不到崩溃,但它们显然一直在传递像素,所以我对释放这些中间状态感到不安。
(我知道根据 CF 文档,我应该在后者上调用 release,因为它们来自 Create 函数,但我可以在使用 UIImage 之前这样做吗?)大概我可以使用提供者的 dealloc 回调来清理像素缓冲区,但还有什么?
谢谢!