【问题标题】:CGContextDrawImage memory is not freedCGContextDrawImage 内存未释放
【发布时间】:2012-12-12 08:20:18
【问题描述】:

我正在使用PhotoScrollerNetwork project 为我的项目中的视图提供单个高分辨率图像并自动平铺它,因此可以正确管理内存。它使用此代码块将完整的高分辨率图像绘制到内存中,以便可以从中计算出图块。

-(void)drawImage:(CGImageRef)image {
       madvise(ims[0].map.addr, ims[0].map.mappedSize - ims[0].map.emptyTileRowSize, MADV_SEQUENTIAL);

       unsigned char *addr = ims[0].map.addr + ims[0].map.col0offset + ims[0].map.row0offset * ims[0].map.bytesPerRow;
       CGContextRef context = CGBitmapContextCreate(addr, ims[0].map.width, ims[0].map.height, bitsPerComponent, ims[0].map.bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);
       assert(context);
       CGContextSetBlendMode(context, kCGBlendModeCopy); // Apple uses this in QA1708
       CGRect rect = CGRectMake(0, 0, ims[0].map.width, ims[0].map.height);
       CGContextDrawImage(context, rect, image);
       CGContextRelease(context);

       madvise(ims[0].map.addr, ims[0].map.mappedSize - ims[0].map.emptyTileRowSize, MADV_FREE);
 }

在类的dealloc 方法中,ims 被释放('free(ims)'),所以应该妥善处理。但是,如果我反复创建一个新视图(并因此调用 drawImage),我的记忆就会被填满。我发现如果我评论CGContextDrawImage(context, rect, image);,内存是ok的,所以我认为内存中保留了一些东西,但我无法得到什么......总是调用dealloc方法,所以这不是问题。

编辑: 我的图片也正常发布了,这是完整的流程:

- (void)myFunc {
        CFDictionaryRef options = [self createOptions];
        CGImageRef image = CGImageSourceCreateImageAtIndex(imageSourcRef, 0, options);
        CFRelease(options);
        CFRelease(imageSourcRef);
        if (image) {
            [self decodeImage:image];
            CGImageRelease(image);
        }
}

- (void)decodeImage:(CGImageRef)image {
    assert(decoder == cgimageDecoder);

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

#if LEVELS_INIT == 0
    zoomLevels = [self zoomLevelsForSize:CGSizeMake(width, height)];
    ims = calloc(zoomLevels, sizeof(imageMemory));
#endif

    [self mapMemoryForIndex:0 width:width height:height];

    [self drawImage:image];
    [self createLevelsAndTile];
}

【问题讨论】:

  • 你为什么不向 github 支持链接发布问题?这里的任何人(但我,该项目的作者)不太可能回答您关于 SO 的问题 :-)。最好的办法可能是将您修改后的项目,压缩,然后将其发布到一个投递箱帐户,以便我可以查看它(但可能要到本周末才会发生)。

标签: ios memory cgcontext


【解决方案1】:

同时运行本地 from-bundle 图像和网络图像,似乎任何重大泄漏都消失了。这适用于 iOS7 和 Xcode 5。

【讨论】:

  • 我有 12 MB 的 jpg 图像,一切都很完美,但 CGContextDrawImage(context, rect, image);导致内存上升到 360 MB,应用程序崩溃。这是因为madvise吗?此函数的任何替代方法 -(void)drawImage:(CGImageRef)image??
  • 好吧,试着评论一下看看。我不知道 - 这个项目在内存严重受限的原始 iPad 上运行。
  • 我尝试删除 madvise,但似乎 CGContextDrawImage 在内部解压缩了整个 jpg,这就是它占用这么多内存的原因。我在 500 MB RAM 的 iPad mini 上运行。及其崩溃。
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2015-06-21
  • 2012-05-22
  • 1970-01-01
相关资源
最近更新 更多