如何正确释放CGImageRef?
我的代码是这样的:
CGImageRef ImageCreateWithFile(NSString *filePath)
{
if(NULL == filePath)
return NULL;
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
if(NULL == data)
return NULL;
CGImageSourceRef isrc = CGImageSourceCreateWithData((CFDataRef)data, NULL);
// [data release];
CGImageRef image = CGImageSourceCreateImageAtIndex(isrc, 0, NULL);
CFRelease(isrc);
isrc = nil;
[data release];
data = nil;
return image;
}
- (IBAction)addClick:(id)sender
{
CGImageRef image = ImageCreateWithFile(@"/Users/user/t.jpg");
if(NULL != image)
_imageList.push_back(image);
}
- (IBAction)removeClick:(id)sender
{
if(_imageList.size() > 0)
{
CGImageRef image = _imageList[0];
CGImageRelease(image);
image = nil;
_imageList.erase(_imageList.begin());
}
}
imageList
声明如下:
std::vector<CGImageRef> _imageList;
这个程序是为了测试释放 CGImageRef
的方法而编写的。当我添加一些图像时,我在活动监视器中看到程序的内存使用量定期上升,但是当我通过 remove
方法删除一个或多个图像后,内存使用量从未下降。不知道为什么释放方法不起作用?有什么帮助吗?谢谢!!!
My Code like this:
CGImageRef ImageCreateWithFile(NSString *filePath)
{
if(NULL == filePath)
return NULL;
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
if(NULL == data)
return NULL;
CGImageSourceRef isrc = CGImageSourceCreateWithData((CFDataRef)data, NULL);
// [data release];
CGImageRef image = CGImageSourceCreateImageAtIndex(isrc, 0, NULL);
CFRelease(isrc);
isrc = nil;
[data release];
data = nil;
return image;
}
- (IBAction)addClick:(id)sender
{
CGImageRef image = ImageCreateWithFile(@"/Users/user/t.jpg");
if(NULL != image)
_imageList.push_back(image);
}
- (IBAction)removeClick:(id)sender
{
if(_imageList.size() > 0)
{
CGImageRef image = _imageList[0];
CGImageRelease(image);
image = nil;
_imageList.erase(_imageList.begin());
}
}
The imageList
declared like:
std::vector<CGImageRef> _imageList;
This program is wrote for testing the method of releasing CGImageRef
. When I add some images, I see the memory usage of the program rises regularly in the Activity Monitor,but after I remove one or more images by the remove
method, the memory never descend. I don't know why the release method not work? Any help? Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看 CGImageSourceCreateImageAtIndex 的文档,您会发现最后一个参数是选项字典。
其中一个选项称为 kCGImageSourceShouldCache。
您可以尝试传递字典,将此选项设置为
false
。下面的代码片段来自图像 I/O 指南 此处
If you look at the documentation of
CGImageSourceCreateImageAtIndex
, you'll see that the last argument is a dictionary of options.One of these options is called
kCGImageSourceShouldCache
.You could try to pass a dictionary, setting this option to
false
.The snippet below is from the Image I/O Guide here