2013-01-25 11 views
5

मुझे एक गाऊशियन ब्लर मिला है जो मैं एक ऐप के लिए कर रहा हूं।गॉसियन ब्लर फ़िल्टर रिवर्सल: आईओएस

//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

के बाद

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

एक संपत्ति

@property (nonatomic, strong) UIImage* originalImage; 

तो में viewImage के लिए सूचक रखें जोड़ने

self.originalImage = viewImage; 

अपनी छवि को वापस जाने के लिए:

newView.image = self.originalImage; 

जब आप धुंध लागू करते हैं तो यह दृश्य इमेज को परिवर्तित नहीं करता है .. आपने एक अलग सीआईएममेज बनाया है जो धुंधला हो जाता है, और आप धुंधले सीआईएममेज से एक नया यूआईएममेज बनाते हैं।

+0

इससे मुझे एहसास हुआ कि मैं जो भी कर रहा हूं वह गलत है। मैं अपना स्वयं का प्रयास कर रहा हूं। अवलोकन धुंध क्योंकि मैं कुछ वस्तुओं को अस्थायी रूप से इसके ऊपर रख रहा हूं। मैं एक मूल छवि नहीं चाहता, लेकिन इसके बजाय इस दृश्य पर वापस जाने के लिए कि मैं मूल रूप से चालू था। आपका जवाब सही है, लेकिन क्या आपको लगता है कि आप इसे प्रतिबिंबित करने के लिए अपना उत्तर संपादित कर सकते हैं? काश मैं आपको इसके लिए दो चेकमार्क दे सकता हूं: पी – 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; 

@ हेवास के उत्तर पर आपकी टिप्पणी के अनुसार, आपको दृश्य को पूरी तरह से धुंधला नहीं करना चाहिए। यदि दृश्य धुंधला हो रहा है, तो कुछ और है जो आप अपने कार्यक्रम में कहीं और गलत कर रहे हैं।

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

  • मूल छवि को उल्टा कर सकते करने के लिए, तुम सिर्फ डाल अन्य छवि में धुंधली प्रतिलिपि देखें जो मूल पर दिखाई देती है। मेरे मामले में मूल छवि एक छवि दृश्य नहीं है, लेकिन दृश्य स्वयं ही है। तो मैं धुंधली छवि को सेट करने के लिए केवल मेरे दृश्य में एक छवि दृश्य जोड़ता हूं, और जब मैं उलटा करना चाहता हूं तो शून्य पर सेट करता हूं।

  • अंत में, यदि आप झपकी से बचने के लिए जब आप धुंधला छवि सेट चाहते हैं, आप एनिमेशन में अल्फा एक नरम संक्रमण बनाने के लिए साथ खेल सकते हैं:

[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]; 

  • यहां मेरा कॉम है 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]; 
        }]; 
} 
संबंधित मुद्दे