简体   繁体   中英

iOS: image filter CIFilter state not maintained

I am trying CIFilter to apply various effects(brightness, contrast, saturation, etc) on an image.

The image filter is applied successfully. But the last state of image edited is not maintained, ie for example: if I increase brightness of image and then apply contrast to the edited image, then the contrast is applied to original image instead of being applied to the brightness increased image

Following is the code:

    - (IBAction)contrastSliderValueChanged:(id)sender {

        filter=NULL;
        result=NULL;
        cgimg=NULL;
        newimg=NULL;



        if (adjustMenuTag==1) {

            filter=[CIFilter filterWithName:@"CIColorControls"];
            [filter setDefaults];
            [filter setValue:beginImage forKey:@"inputImage"];
            [filter setValue:[NSNumber numberWithFloat:contrastSlider.value] forKey:@"inputBrightness"] ;
            result=[filter valueForKey:kCIOutputImageKey];

            cgimg=[context createCGImage:result fromRect:[result extent]];
            newimg=[UIImage imageWithCGImage:cgimg];


            imageViewDisplay.image=newimg;
            beginImage = result;
            CFRelease(cgimg);
    //        beginImage = [[CIImage alloc] initWithImage:imageViewDisplay.image];


        } else if(adjustMenuTag ==2 ) {

            filter=[CIFilter filterWithName:@"CIColorControls"];
            [filter setDefaults];
            [filter setValue:beginImage forKey:@"inputImage"];
            [filter setValue:[NSNumber numberWithFloat:contrastSlider.value+0.5] forKey:@"inputContrast"] ;
            result=[filter valueForKey:kCIOutputImageKey];
            cgimg=[context createCGImage:result fromRect:[result extent]];
            newimg=[UIImage imageWithCGImage:cgimg];
            imageViewDisplay.image=newimg;
            beginImage = result;
            CFRelease(cgimg);
    //        beginImage = newimg.CIImage;  //this also doesn't work.
        }
}

Where am I going wrong? How do I solve this? How do I maintain the state the edited image?

Looking at the code, it seems like you are creating a new instance of a CIColorControls filter every time a menu item is selected. You should create one, keep it around, and update its inputs as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM