0

我正在使用以下代码来应用图像过滤器。在我的应用程序中,我正在过滤亮度、对比度和饱和度。我使用三个单独的滑块来更改值。当我继续移动这些值时,内存消耗超过 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;
}
4

2 回答 2

0

我不确定您的 setImage 方法在做什么,但我会在 performSelector 之前移动 CGImageRelease(tempImage)。

于 2013-10-31T11:57:09.950 回答
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,因此不太可能因过度消耗内存而崩溃。但是,如果你阻塞主线程太久,看门狗定时器会通过后门将其取出并直接射中头部。

使用工具来监控您的堆大小并尝试找出根本原因。

于 2013-10-31T11:41:47.437 回答