UIImageView에 저장된 이미지를 가져 와서 다른 UIImageView의 새로운 이미지로 복사하기위한 내용을 수정하는 코드 세트가 있습니다. 문제는 프로젝트를 분석 할 때 항상이 컴파일러에서 메모리 경고를받습니다. 다양한 방법으로이 코드를 구현하려고 시도했지만 항상 다른 종류의 메모리 경고를 받게됩니다. 컴파일러의 출력에 따르면 "함수 호출 'CGBitMapContextCreateImage'가 +1 보유 계수가있는 핵심 기초 개체를 반환하므로 이미지 개체의 보유 개수가 +1이됩니다. 만약 이미지 객체를 자동으로 풀어 준다면, 컴파일러는 autorelease가 여러 번 호출되고 이미지 객체의 원래 보유 수는 0이었던 메모리 경고를 가지고 있습니다.CGContextRef로 iOS에서 메모리 누수가 발생했습니다.
이 두 컴파일러 경고는 상반되지 않습니까? 메모리 누수가 발생하지 않도록이 코드를 어떻게 수정할 수 있습니까?
-(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;
}
신경 끄시 고, 내가 컨텍스트에서 만든의 CGImage 해제하려면 다음 코드를 추가하여 고정 :
CGImageRef imageRef = CGBitmapContextCreateImage(bmContext);
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
하하 고마워요, 당신의 코멘트를 보지 못했습니다, 나는 그것을 몇 분 전에 알아 냈습니다! 당신의 도움을 주셔서 감사합니다! – Andrew