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".
Items in bold are (or will be) leaks. See the properties link in my signature for more info.
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);
}
I was already thinking in that direction but the problem is that I don't know how to release the Cgimages, hence I'm using "CGImageRelease(cgImage);", don't even know if that's the right method. I checked your signature link, but it hasn't made me any wiser.
I've tried and it works fine, but my images are deformed anyway, so I will give up. Seems an iPhone isn't really made to perform this kind of graphical operations anyway. It has nothing to do with the memory leaks apparently, it are just the images the iPhone's camera generates that are reacting bad to the filters.