So I'm making some filters for my iPhone app and everything goes fine as long as I work with small images, but when I bring in the big life size ones like the one the iPhone camera shoots, I'm experiencing some errors probably caused by memoryleaks or resources not released correctly.
So this is my code for one filter :
PHP Code:
- (IBAction)MonoImage {
//previoushit checks if a filter was already activated and if it is, it restores it and before applying the new filter, if it isn't it saves the new image to an UIimage called "copyOfimage2".
if (previousHit == 1)
{
CGImageRef cgImage = [copyOfImage2 CGImage];
image.image = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
}
else
{
CGImageRef cgImage = [image.image CGImage];
copyOfImage2 = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
}
previousHit = 1;
CGImageRef inImage = image.image.CGImage;
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
Byte tmpByte;
int length = CFDataGetLength(m_DataRef);
for (int index = 0; index < length; index += 4)
{
tmpByte = (m_PixelBuf[index + 1] + m_PixelBuf[index + 2] + m_PixelBuf[index + 3]) / 3;
if (tmpByte >= 128)
m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 255;
else
m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 0;
}
ctx = CGBitmapContextCreate(m_PixelBuf,
CGImageGetWidth( inImage ),
CGImageGetHeight( inImage ),
8,
CGImageGetBytesPerRow( inImage ),
CGImageGetColorSpace( inImage ),
kCGImageAlphaPremultipliedFirst );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
image.image = rawImage;
CFRelease(m_DataRef);
CGImageRelease(imageRef);
}
So I presume some CGimageRefs aren't released properly so I'm trying to find out a way to optimize this code to release them.
I also have some of these filters tied to an UISlider, so this can be annoyingly slow in this way and can even cause the app to crash.
Can anyone help?