尝试使用CIFilter修改亮度/对比度会使应用程序崩溃

时间:2012-01-19 10:53:33

标签: objective-c ios filter uiimage

我有两个UIImageViews,每个都包含一个UIImage。顶部的UIImageView有一个UIImage,其对比度/亮度我要根据按下的两个按钮中的哪一个进行修改。我把CIFilter,CIContext和CIImage放在我的ViewController的@implementation中,在viewDidLoad中初始化它们是这样的:

controlsFilter = [CIFilter filterWithName:@"CIColorControls"];
newContext = [CIContext contextWithOptions:nil];
beginImage = [[CIImage alloc] initWithImage:imageView.image];
[controlsFilter setValue:beginImage forKey: kCIInputImageKey];

然后,我按下对比度或亮度的按钮具有以下操作实现:

- (IBAction) contrast
{
    [slider setMaximumValue:4.0];
    [slider setMinimumValue:0.25];
    [slider setValue:1.0];
    if (contrast == NO) {
        contrast = YES;
        brightness = NO;
        [slider setHidden:NO];
    }
    else {
        contrast = NO;
        [slider setHidden:YES];
    }    
}

- (IBAction) brightness {

    [slider setMaximumValue:1.0];
    [slider setMinimumValue:-1.0];
    [slider setValue:0.0];
    if (brightness == NO) {
        contrast = NO;
        brightness = YES;
        [slider setHidden:NO];
    }
    else {
        brightness = NO;
        [slider setHidden:YES];
    }
}

直到这里,一切似乎都很好,但是当我尝试在这里修改图像时:

-(IBAction)changeValue:(UISlider *)sender {
    float slideValue = [sender value];

    if (brightness == YES)
        [controlsFilter setValue:[NSNumber numberWithFloat:slideValue]  forKey:@"inputBrightness"];
    if (contrast == YES)
        [controlsFilter setValue:[NSNumber numberWithFloat:slideValue] forKey:@"inputContrast"];
    CIImage *outputImage = [controlsFilter outputImage];

    CGImageRef cgimg = [newContext createCGImage:outputImage fromRect:[outputImage extent]];

    UIImage *newImg = [UIImage imageWithCGImage:cgimg];    
    [imageView setImage:newImg];

    CGImageRelease(cgimg);
}

显然,如果我删除底部的UIImageView,一切正常。当我有两个UIImageViews,它崩溃,我不知道如何解决它。我只想获得第二个视图的上下文,而不是全部。

0 个答案:

没有答案