0

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);

}
Imad Ali
  • 3,261
  • 1
  • 25
  • 33
  • in your action every time you are fetching image from self.affectView.image as well as assigning new image to it. Hence each time you get new image instead of original image. You need to add effect to your original image rather than the updated one.I have added ans for your refernce. – luckyShubhra Jul 18 '17 at 11:40

2 Answers2

0

You are getting your demo image from _affectView, applying a filter to it and then saving it in _affectView again. The next time you retrieve _affectView into a UIImage it will not be the original one but the modified one so it is imposible to get back to the original.

Just at the start (outside this method) save your original image in demo and modify demo every time you want.

Miguel Isla
  • 1,379
  • 14
  • 25
  • How can i show the effect of the filter on the imageview if i dont assign the output image from filter to imageview? – jyotsna pandey Jul 18 '17 at 07:22
  • You use a global variable named demo, apply the filter to it and the assign that filtered image to the imageview. You don't have to get `demo` overridden every time you want to apply filters because once you apply the filter there is no coming back to the original image. – Miguel Isla Jul 18 '17 at 07:29
  • where to assign this demoimage to _affectView,so that i can see the changes ? – jyotsna pandey Jul 18 '17 at 07:29
  • at view didLoad-demoimage = affectView.Image,after that slide change,where I will write affectview.image = image – jyotsna pandey Jul 18 '17 at 07:37
  • I guess you are selecting somewhere the image you want to apply the filter, I don't know how without having more code but in my opinion, the place where you select which image you want to filter or the place where you assign the first time the image to the imageview are the best choices to save `demo`. – Miguel Isla Jul 18 '17 at 07:39
  • The place where you write to `_affectView` is correct. – Miguel Isla Jul 18 '17 at 07:41
  • I am just selecting slider and want to see change on image view as per slider moves – jyotsna pandey Jul 18 '17 at 08:51
  • Just remove `demoImage = _affectView.image;` from `- (IBAction)actionSharp:(UISlider *)sender` and everything should work properly. If you do not see anything changing maybe the assignment of `demo` in `viewDidLoad` is not correct if `_affectView` is null or empty. – Miguel Isla Jul 18 '17 at 09:01
  • give me ur mail id ..will send my project – jyotsna pandey Jul 18 '17 at 10:30
  • Post some code here so more people can help you. Not all the project, of course, but the code you think will help to solve your problem. I am sorry but i am not going to give an email. – Miguel Isla Jul 18 '17 at 11:29
0

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);
}
luckyShubhra
  • 2,731
  • 1
  • 12
  • 19