【问题标题】:iOS iPhone CGImage Memory problemsiOS iPhone CGImage 内存问题
【发布时间】:2012-09-18 21:23:19
【问题描述】:

我正在尝试创建我拥有的一些数据的可视化表示。

我拥有的功能可以完美地(并且非常快速地)创建图像,但在仪器下,Real Memory 的使用量猛增,最终导致应用程序崩溃。

我用return [UIImage imageNamed:@"blah"]; 替换了我的函数,内存问题完全消失了。

想知道是否有人可以看到内存被占用的原因和位置,以及我如何才能再次释放它?

我已经在分配和泄漏工具下运行了工具,但那里没有任何显示。

函数是……

- (UIImage*)imageOfMapWithDeadColor:(UIColor *)deadColor aliveColor:(UIColor *)aliveColor
{
//translate colours into rgb components
if ([deadColor isEqual:[UIColor whiteColor]]) {
    dr = dg = db = 255;
} else if ([deadColor isEqual:[UIColor blackColor]]) {
    dr = dg = db = 0;
} else {
    [deadColor getRed:&drf green:&dgf blue:&dbf alpha:&blah];

    dr = drf * 255;
    dg = dgf * 255;
    db = dbf * 255;
}

if ([aliveColor isEqual:[UIColor whiteColor]]) {
    ar = ag = ab = 255;
} else if ([aliveColor isEqual:[UIColor blackColor]]) {
    ar = ag = ab = 0;
} else {
    [aliveColor getRed:&arf green:&agf blue:&abf alpha:&blah];

    ar = arf * 255;
    ag = agf * 255;
    ab = abf * 255;
}

//create bytes of image from the cell map
int yRef, cellRef;

unsigned char *cell_ptr = cells;

for (int y=0; y<self.height; y++)
{
    yRef = y * (self.width * 4);

    int x = 0;
    do
    {
        cellRef = yRef + 4 * x;

        if (*cell_ptr & 0x01) {
            //alive colour
            buffer[cellRef] = ar;
            buffer[cellRef + 1] = ag;
            buffer[cellRef + 2] = ab;
            buffer[cellRef + 3] = 255;
        } else {
            //dead colour
            buffer[cellRef] = dr;
            buffer[cellRef + 1] = dg;
            buffer[cellRef + 2] = db;
            buffer[cellRef + 3] = 255;
        }
        cell_ptr++;
    } while (++x < self.width);
}

//create image
imageRef = CGImageCreate(self.width, self.height, 8, 32, 4 * self.width, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

UIImage *image = [UIImage imageWithCGImage:imageRef];

//return image
return image;
}

一些笔记...

buffer 是 init 中的 ivar GLubyte malloc-ed。 cells 是我从中获取数据的原始 unsigned char 数组。

就像我说的,该功能完美运行(创建图像等...)我只是占用了大量内存。

哦...这个函数被称为 LOTS,比如每秒 30 次或更多(我知道这会有所作为)。

感谢您的帮助。

【问题讨论】:

    标签: iphone ios memory cgimage


    【解决方案1】:

    您使用 CGImageCreate 并且文档说:

    返回值 一个新的 Quartz 位图图像。您负责通过调用 CGImageRelease 释放此对象。

    【讨论】:

    • 太棒了!谢谢!我不知道我是怎么错过的。现在完美运行。 (当计时器用完时将接受为正确答案)
    猜你喜欢
    • 2017-06-23
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2011-11-12
    • 2011-04-11
    相关资源
    最近更新 更多