iosobjective-cuiimageviewcore-imageluminance

Imageview not resetting to original after sharpness filter used


Not able to get original image after applying the filter on imageview.As I want to get original image after moving the slider from 1 to 0 (max to min value, I mean in reverse direction). Below is the code for applying sharpness effect

- (IBAction)actionSharp:(UISlider *)sender {


    demoImage = _affectView.image;
    CIImage* image = [CIImage imageWithCGImage:demoImage.CGImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    NSNumber *testNSNumber = [NSNumber numberWithFloat:_sharpActionWithSlider.value];
    NSLog(@"ffghhn %f",_sharpActionWithSlider.value);
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CISharpenLuminance" keysAndValues: @"inputImage", image, nil];

    [gaussianBlurFilter setDefaults];
    [gaussianBlurFilter setValue:testNSNumber forKey:kCIInputSharpnessKey];
    [gaussianBlurFilter setDefaults];
    CIImage *result2 = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result2 fromRect:[result2 extent]];
    UIImage *sharp= [UIImage imageWithCGImage:cgImage];
    UIImage *p = sharp;
    self.affectView.image= p;
    self.affectView.alpha = sender.value;
   CGImageRelease(cgImage);

}

Solution

  • Just copy the image on which you are adding filter and keep it aside. Add effects to copied image and show them on your _affectView image view.

    On your slider action every time get reference from your original image and effect to that image rather than image from image view.

    Create global var as mainImage as

    UIImage *mainImage;
    

    in your viewDidLoad assign origial image to your main image

    mainImage = [UIImage imagenamed:"yourMainImage"]; // assign from source local or url
    

    In you IBAction on every slider action get refernce of original image instead of fetching image from imageview.

    - (IBAction)actionSharp:(UISlider *)sender {
    UIImage *demoImage = mainImage;
    CIImage* image = [CIImage imageWithCGImage:demoImage.CGImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    NSNumber *testNSNumber = [NSNumber numberWithFloat:_sharpActionWithSlider.value];
    NSLog(@"ffghhn %f",_sharpActionWithSlider.value);
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CISharpenLuminance" keysAndValues: @"inputImage", image, nil];
    
    [gaussianBlurFilter setDefaults];
    [gaussianBlurFilter setValue:testNSNumber forKey:kCIInputSharpnessKey];
    [gaussianBlurFilter setDefaults];
    CIImage *result2 = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result2 fromRect:[result2 extent]];
    UIImage *sharp= [UIImage imageWithCGImage:cgImage];
    UIImage *p = sharp;
    self.affectView.image= p;
    self.affectView.alpha = sender.value;
    CGImageRelease(cgImage);
    }