如何正确释放CGImageRef?

发布于 2024-12-06 19:09:33 字数 1125 浏览 0 评论 0原文

我的代码是这样的:

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 技术交流群。

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

发布评论

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

评论(1

∝单色的世界 2024-12-13 19:09:33

如果您查看 CGImageSourceCreateImageAtIndex 的文档,您会发现最后一个参数是选项字典。

其中一个选项称为 kCGImageSourceShouldCache。

您可以尝试传递字典,将此选项设置为false
下面的代码片段来自图像 I/O 指南 此处

CFStringRef       myKeys[1];
CFTypeRef         myValues[1];
CFDictionaryRef   myOptions;

myKeys[0] = kCGImageSourceShouldCache;
myValues[0] = (CFTypeRef)kCFBooleanFalse;

myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
               (const void **) myValues, 1,
               &kCFTypeDictionaryKeyCallBacks,
               & kCFTypeDictionaryValueCallBacks);

CGImageRef image = CGImageSourceCreateImageAtIndex(isrc, 0, myOptions);

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

CFStringRef       myKeys[1];
CFTypeRef         myValues[1];
CFDictionaryRef   myOptions;

myKeys[0] = kCGImageSourceShouldCache;
myValues[0] = (CFTypeRef)kCFBooleanFalse;

myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
               (const void **) myValues, 1,
               &kCFTypeDictionaryKeyCallBacks,
               & kCFTypeDictionaryValueCallBacks);

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