2013-01-25 72 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]; 
        }]; 
}