我使用以下代码来应用图像过滤器。在我的应用程序中,我正在过滤亮度,对比度和饱和度。我使用三个单独的滑块来改变值。随着我继续移动值,内存消耗超过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;
}
答案 0 :(得分:0)
你不应该在主线程中进行繁重的图像处理。除非你已经实现了多线程(你的代码片段中没有提到),否则请这样做。
您可以尝试:
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.yourorg", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(backgroundQueue, ^
{
// setBrightnessAndContrastOf method goes here
dispatch_sync(mainQueue, ^ {
//notify main thread about process status
});
});
由于您正在使用ARC,因此不太可能因过度消耗内存而导致崩溃。但是,如果你阻塞主线程太长时间,看门狗定时器会通过后门将其取出并直接射入头部。
使用仪器监控堆大小并尝试找出根本原因。
答案 1 :(得分:0)
我不确定你的setImage方法在做什么,但我会在performSelector之前移动CGImageRelease(tempImage)。