Search code examples
iosobjective-ccifilter

Reset button to original image from filtered image


I tried to add filters to my image in image view, when i tried to click on filter buttons they are stacking. Can you help me to create a reset or undo button to go to original image with iam getting original image from camera and camera roll?

(IBAction)filter:(id)sender {
CIContext *context=[CIContext contextWithOptions:nil];
CIImage *image =[CIImage imageWithCGImage:imageview.image.CGImage];

CIFilter *filter =[CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:@1.0f forKey:@"InputIntensity"];

CIImage *result =[filter valueForKey:kCIOutputImageKey];


CGImageRef cgImage =[context createCGImage:result fromRect:result.extent];

UIImage *uiImage =[[UIImage alloc]initWithCGImage:cgImage];
[self.imageview setImage:uiImage];

Solution

  • Your filters are stacking because you're modifying the image in the imageview, and then putting that modified image back into the imageview. You should store your original image in a separate property and always start your modifications from that. For example, a property like:

    @property (nonatomic, strong) UIImage *originalImage;
    

    Then in your method, use the original image instead of the one already in the image view:

    CIImage *image = [CIImage imageWithCGImage:self.originalImage.CGImage];
    

    Storing data in member variables/properties is also just better practice than storing data in the view.