2013-01-25 74 views
5

我有一个高斯模糊,我正在做一个应用程序。高斯模糊滤镜逆转:iOS

//Get a UIImage from the UIView 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //Blur the UIImage 
    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage]; 
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
    [gaussianBlurFilter setValue:imageToBlur forKey:@"inputImage"]; 
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat:2] forKey:@"inputRadius"]; 
    CIImage *resultImage = [gaussianBlurFilter valueForKey:@"outputImage"]; 
    UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage]; 

    //Place the UIImage in a UIImageView 
    newView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    newView.image = endImage; 
    [self.view addSubview:newView]; 

它工作的很好,但我希望能够扭转它,并返回到正常的视图。

我该怎么做,因为试图从模板的视图中删除模糊的视图不起作用。我也尝试将各种属性设置为零,但没有运气。

+0

你就不能确保应用模糊之前viewImage的副本,当你想反向恢复? – Ravi

+0

一旦模糊应用后,如何在不随后更改两个副本的情况下制作副本? – jakenberg

回答

3

保持一个指向viewImage在属性

@property (nonatomic, strong) UIImage* originalImage; 

再经过

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

添加

self.originalImage = viewImage; 

要恢复你的形象:

newView.image = self.originalImage; 

当您应用模糊时,它不会改变viewImage ..您创建了一个单独的CIImage,它会变得模糊,并且您会从模糊的CIImage创建一个新的UIImage。

+0

这让我意识到,我整体做的事情是不正确的。我试图让我的self.view模糊,因为我把一些对象暂时放在它的顶部。我不想要一张原始图像,而是回到最初的视图。你的回答是正确的,但你认为你可以编辑你的答案来反映这一点吗?我希望我可以给你两个checkmark:P – jakenberg

+0

我最终创建了一个模糊的UIView,然后摆脱它。谢谢! – jakenberg

0

就像我在之前的评论中所说的那样,您可以创建一个属性/ iVar,在应用效果之前保存图像,并在需要撤消时恢复。

if(!_originalImage) 
    _originalImage = [[UIImage alloc] init]; 

_originalImage = viewImage; //(Creates a copy, not a C-Type pass-by-reference) 

// Do your Blur Stuff 

// Now somewhere down the line in your program, if you don't like the blur and the user would like to undo: 

viewImage = _originalImage; 

根据您对@ HeWas的回答的评论,您不应该完全模糊视图。如果视图变得模糊,那么在程序的其他地方还有其他问题。

1
  • 我在代码中看到的第一件事是,您不在异步任务中应用过滤器。模糊图像需要时间,所以如果你不想冻结你应该使用主线程:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    //blur the image in a second thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //set the blurred image to your imageView in the main thread 
    }); 
}); 

  • 为了能够逆转原始图像,你只需把在原始图像上出现的其他图像模糊副本。在我的情况下,原始图像不是imageView,而是视图本身。所以我只在我的视图中添加一个imageView来设置模糊的图像,并且当我想要反转时将其设置为零。

  • 最后,如果你想避免闪烁,当你设置的模糊图像,你可以用α的动画制作软过渡玩:

[self.blurredImageView setAlpha: 0.0]; //make the imageView invisible 
[self.blurredImageView setImage:blurredImage]; 
//and after set the image, make it visible slowly. 
[UIView animateWithDuration:0.5 delay:0.1 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          [self.blurredImageView setAlpha: 1.0]; 
         } 
         completion:nil]; 

  • 这是我的com plete方式:

- (void)makeBlurredScreenShot{ 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CIImage *sourceImage = [CIImage imageWithCGImage:imageView.CGImage]; 

    // Apply clamp filter: 
    // this is needed because the CIGaussianBlur when applied makes 
    // a trasparent border around the image 
    NSString *clampFilterName = @"CIAffineClamp"; 
    CIFilter *clamp = [CIFilter filterWithName:clampFilterName]; 
    if (!clamp) 
     return; 

    [clamp setValue:sourceImage forKey:kCIInputImageKey]; 
    CIImage *clampResult = [clamp valueForKey:kCIOutputImageKey]; 

    // Apply Gaussian Blur filter 
    NSString *gaussianBlurFilterName = @"CIGaussianBlur"; 
    CIFilter *gaussianBlur   = [CIFilter filterWithName:gaussianBlurFilterName]; 
    if (!gaussianBlur) 
     return; 

    [gaussianBlur setValue:clampResult forKey:kCIInputImageKey]; 
    [gaussianBlur setValue:[NSNumber numberWithFloat:8.0] forKey:@"inputRadius"]; 

    CIImage *gaussianBlurResult = [gaussianBlur valueForKey:kCIOutputImageKey]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     CGImageRef cgImage = [context createCGImage:gaussianBlurResult fromRect:[sourceImage extent]]; 

     UIImage *blurredImage = [UIImage imageWithCGImage:cgImage]; 
     CGImageRelease(cgImage); 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.blurredImageView setAlpha: 0.0]; 
      [self.blurredImageView setImage:blurredImage]; 
      [UIView animateWithDuration:0.5 delay:0.1 
           options:UIViewAnimationOptionCurveEaseInOut 
          animations:^{ 
           [self.blurredImageView setAlpha: 1.0]; 
          } 
          completion:nil]; 
     }); 
    }); 
} 

- (void)removeBlurredScreenShot{ 
    [UIView animateWithDuration:0.5 delay:0.1 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
          [self.blurredImageView setAlpha: 0.0]; 
          } 
        completion:^(BOOL finished) { 
          [self.blurredImageView setImage:nil]; 
        }]; 
}