创建随机图像CoreGraphics泄漏内存

时间:2016-09-08 04:18:31

标签: ios c memory-leaks core-graphics

在我的节目中, 我有一个创建随机生成图像的方法, 只是注意到仪器会泄漏记忆,但我不确定原因。

这是泄漏的部分:

int * getRandomImage(int width, int height) {
int * bytes = new int[height * width]; //this line is pointed by instruments

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        bytes[i * width + j] = arc4random();
    }
}

    return bytes
}





-(void) loadRandomImage {
const int height = 400;
const int width = 400;
int * bytes = getRandomImage(width, height);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bytes, width * height, NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;

CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

CGDataProviderRelease(provider);

UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];

self.imageView.image = newUIImage;

CGImageRelease(imageRef);
}

0 个答案:

没有答案
相关问题