The following snippet of code save a CIImage
to disk using an UIImage
.
- (void)applicationWillResignActive:(UIApplication *)application
{
NSString* filename = @"Test.png";
UIImage *image = [UIImage imageNamed:filename];
// make some image processing then store the output
CIImage *processedImage = [CIImage imageWithCGImage:image.CGImage];
#if 1// save using context
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgiimage = [context createCGImage:processedImage fromRect:processedImage.extent];
image = [UIImage imageWithCGImage:cgiimage];
CGImageRelease(cgiimage);
#else
image = [UIImage imageWithCIImage:processedImage];
#endif
// save the image
NSString *filePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[@"../Documents/" stringByAppendingString:filename]];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
}
However, it leaks the CGImageRef
even when it is released by calling CGImageRelease
If the line with #if 1
is changed to #if 0
, the UIImage
is created directly from the CIImage
and there are no memory leaks, but then the UIImage
is not saved to disk