[英]Memory Leak in iOS with CGContextRef
I have a set of code that takes an image stored in a UIImageView and modify's its contents to copy into a new image for a different UIImageView. 我有一组代码,该代码需要将图像存储在UIImageView中并修改其内容以将其复制到其他UIImageView的新图像中。 The problem is that this code always receives a memory warning from my compiler when I analyze the project.
问题在于,当我分析项目时,此代码始终会收到来自编译器的内存警告。 I have tried to implement this code in a variety of ways and I always end up receiving a different kind of memory warning.
我试图以多种方式实现此代码,但最终总是收到不同类型的内存警告。 The output from the compiler says that the "Call to function 'CGBitMapContextCreateImage' returns a core foundation object with a +1 retain count", and this causes the image object to have a retain count of +1.
编译器的输出显示“对函数'CGBitMapContextCreateImage的调用返回的核心基础对象的保留计数为+1”,这将导致图像对象的保留计数为+1。 If I autorelease the image object the compiler has a memory warning that autorelease is called to many times and the image object originally had a retain count of 0.
如果我自动释放图像对象,则编译器会发出内存警告,提示自动释放被多次调用,并且图像对象最初的保留计数为0。
Aren't these two compiler warnings contradictory? 这两个编译器警告不是矛盾的吗? How can I fix this code as to ensure no memory leaks occur?
如何修复此代码,以确保不发生内存泄漏?
-(UIImage *) makeImageLight{
UIImage * image = self.masterImage.image;
NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = width * bytesPerPixel;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = height}, image.CGImage);
UInt8* data = (UInt8*)CGBitmapContextGetData(bmContext);
for (size_t i = 0; i < CGBitmapContextGetWidth(bmContext); i++)
{
for (size_t j = 0; j < CGBitmapContextGetHeight(bmContext); j++)
{
int pixel = j * CGBitmapContextGetWidth(bmContext) + i;
pixel = pixel * 4;
UInt8 red = data[pixel + 1]; // If you need this info, enable it
UInt8 green = data[pixel + 2]; // If you need this info, enable it
UInt8 blue = data[pixel + 3]; // If you need this info, enable it
red = ((255 - red) * .3) + red;
green = ((255 - green) * .3) + green;
data[pixel + 1] = red;
data[pixel + 2] = green;
data[pixel + 3] = blue;
}
}
// memory warning occurs in the following line:
image = [UIImage imageWithCGImage:CGBitmapContextCreateImage(bmContext)];
CGContextRelease(bmContext);
return image;
}
Nevermind, I fixed it by adding the following code to release the CGImage created from the Context: 没关系,我通过添加以下代码来释放从Context创建的CGImage来解决此问题:
CGImageRef imageRef = CGBitmapContextCreateImage(bmContext);
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
You use CGBitmapContextCreateImage() to create a CGImage, but you haven't released that CGImage 您使用CGBitmapContextCreateImage()创建CGImage,但尚未发布该CGImage。
You need to split the UIImage
creation line as follows: 您需要按如下所示拆分
UIImage
创建行:
CGImageRef cgimage = CGBitmapContextCreateImage(bmContext);
image = [UIImage imageWithCGImage:cgimage];
CGImageRelease(cgimage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.