以下代码按我想要的方式工作,但每次我调用它时,Instruments 都会告诉我有一个 CGImage 内存泄漏。我一直无法理解要发布什么以及何时发布。以下来自我文件的@interface 部分。
CGImageRef depthImageRef;
char *depthPixels;
NSData *depthData;
在接下来的代码中,我基本上会更改 depthPixels,然后将结果存储在新的 depthImageRef 中。
size_t width = CGImageGetWidth(depthImageRef);
size_t height = CGImageGetHeight(depthImageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(depthImageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(depthImageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(depthImageRef);
for (int row = 0; row < height; row += 1) {
for (int bitPlace = 0; bitPlace < bytesPerRow; bitPlace += 4) {
CGPoint pointForHeight = CGPointMake((bitPlace/4) - place.x, row - place.y);
int distanceFromLocation = sqrt(pow(place.x - pointForHeight.x, 2) + pow(place.y - pointForHeight.y, 2));
int newHeight = blopHeight - (5 - sizeSlider.value)*distanceFromLocation;
NSInteger baseBitPlace = row*bytesPerRow + bitPlace;
CGFloat currentHeight = depthPixels[baseBitPlace];
if (newHeight > currentHeight) {
depthPixels[baseBitPlace] = newHeight;
}
}
}
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(depthImageRef);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, depthPixels, [depthData length], NULL);
depthImageRef = CGImageCreate (
width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorspace,
bitmapInfo,
provider,
NULL,
false,
kCGRenderingIntentDefault
);
CGColorSpaceRelease(colorspace);
CFRelease(provider);
CGDataProviderRelease(provider);
我相信泄漏是因为我一直在创建 depthImageRef 但从未释放它。我尝试将 CGImageRelease(depthImageRef) 放在不同的地方并将 depthImageRef 设置为 nil,通常当我这样做时我会崩溃。谢谢!