I'm trying to implement a sepia filter on an image loaded into the UI View from the photo library.

I have a UI Slider...range -1 to 1. And applying the effect Sepia.

The problem I'm having is, the over all effect is getting summed up. Like...if I put slider to 1, then move it back to 0. It doesn't bring the image back to the original form. And I move it back to 1..total sepia is applied 2 times.

- (IBAction)slidermove:(id)sender {

    currentValue=_sliderValue.value;

    if (currentButton==1)
    {
        //1
        CIImage *beginImage = [CIImage imageWithData: UIImagePNGRepresentation(self.imageView.image)];

        //2
        CIContext *context = [CIContext contextWithOptions:nil];

        //3
        CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
                                      keysAndValues: kCIInputImageKey, beginImage,
                            @"inputIntensity", [NSNumber numberWithFloat:currentValue], nil];

        //4
        CIImage *outputImage = [filter outputImage];

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

        //5
        self.imageView.image = [UIImage imageWithCGImage:cgimg];

        //6
        CGImageRelease(cgimg);
    }


}



- (IBAction)applyeffect:(id)sender
{
    currentButton=1;


}
有帮助吗?

解决方案

The problem is occurring because you don't save the original image. You change the image each time the slider changes, so the effect compounds. Here is what you should do instead:

In your @interface:

@property(strong, nonatomic)UIImage *originalImage;

In your @implementation:

-(void)viewDidLoad{
    [super viewDidLoad];
    self.imageView.image = self.originalImage;
}

Then when you do the effect, apply it to the originalImage property and show the result in self.imageView.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top