简体   繁体   English

iOS:图像过滤器CIFilter状态未维护

[英]iOS: image filter CIFilter state not maintained

I am trying CIFilter to apply various effects(brightness, contrast, saturation, etc) on an image. 我正在尝试CIFilter在图像上应用各种效果(亮度,对比度,饱和度等)。

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. 查看代码,似乎每次选择菜单项时都在创建CIColorControls过滤器的新实例。 You should create one, keep it around, and update its inputs as needed. 您应该创建一个,保留它,并根据需要更新其输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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