我正在使用以下代码来应用图像过滤器。在我的应用程序中,我正在过滤亮度、对比度和饱和度。我使用三个单独的滑块来更改值。当我继续移动这些值时,内存消耗超过 1.5 GB 并崩溃。有没有办法减少这种内存消耗以实现无崩溃实现?
(void)setBrightnessAndContrastOf:(UIImage *)image { // forTarget:(UIImageView *)imgView {
if (!image) {
return;
}
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *exposureAdjustmentFilter = [CIFilter filterWithName:@"CIColorControls"];
[exposureAdjustmentFilter setDefaults];
[exposureAdjustmentFilter setValue:inputImage forKey:@"inputImage"];
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:self.contrastValue] forKey:@"inputContrast"]; //default = 1.00
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:self.brightnessValue] forKey:@"inputBrightness"]; //default = 0.00
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:self.saturationValue] forKey:@"inputSaturation"]; //default = 1.00
CIImage *outputImage = [exposureAdjustmentFilter valueForKey:@"outputImage"];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef tempImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:tempImage];
[imageView performSelectorOnMainThread:@selector(setImage:) withObject:newImage waitUntilDone:NO];
CGImageRelease(tempImage);
inputImage = nil;
context = nil;
outputImage = nil;
exposureAdjustmentFilter = nil;
}