1

I am trying to create a blured background when I show a popover I almost got it to work.

I found some Objective C which I translated into C#.

the problem is in this line

UIImage finalImage = new UIImage(resultImage);

hope it is just something I am doing wrong

    private UIView CreateBlurView(UIView view)
    {
        UIGraphics.BeginImageContext(view.Bounds.Size);
        view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
        UIImage viewImage = UIGraphics.GetImageFromCurrentImageContext();

        // Blur Image
        CIImage imageToBlur = CIImage.FromCGImage(viewImage.CGImage);
        CIFilter gaussianBlurFilter = CIFilter.FromName("CIGaussianBlur");
        gaussianBlurFilter.SetValueForKey(imageToBlur,new NSString("inputImage"));
        gaussianBlurFilter.SetValueForKey(new NSNumber(10.0f),new NSString("inputRadius"));
        CIImage resultImage = (CIImage) gaussianBlurFilter.ValueForKey(new NSString("outputImage"));

        UIImage finalImage = new UIImage(resultImage);
        UIImageView  imageView = new UIImageView(view.Bounds);
        imageView.Image = finalImage;
        return imageView;
    }
poupou
  • 43,413
  • 6
  • 77
  • 174
canderse
  • 299
  • 1
  • 15
  • Help us help you :-) It would be easier if you could provide a link to the original ObjC code *and* if you could describe what you *almost* achieved (compared to what you exepcted). – poupou Dec 07 '12 at 00:46
  • Sorry I used this stack overflow article as a reference. http://stackoverflow.com/questions/11130923/apply-blur-to-uiview regards Christian – canderse Dec 07 '12 at 01:26

1 Answers1

3

First make sure you're using iOS 6 (or later) since CIGaussianBlur was not available in iOS before that version.

Next, you're missing (from the original code) the line:

UIGraphicsEndImageContext();

which in C# / MonoTouch should be:

MonoTouch.UIKit.UIGraphics.EndImageContext ();

Finally you hit a bug :(

-[UIImage initWithCIImage]: unrecognized selector sent to instance 0x168f2280

a : is missing at the end of the selector. Now there's another .ctor that accept a CIImage along with a float (scale) and a UIImageOrientation. This one should work properly.

The full source would be:

private UIView CreateBlurView(UIView view)
{
    UIGraphics.BeginImageContext(view.Bounds.Size);
    view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
    UIImage viewImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext ();

    // Blur Image
    CIImage imageToBlur = CIImage.FromCGImage(viewImage.CGImage);
    CIFilter gaussianBlurFilter = CIFilter.FromName("CIGaussianBlur");
    gaussianBlurFilter.SetValueForKey(imageToBlur,new NSString("inputImage"));
    gaussianBlurFilter.SetValueForKey(new NSNumber(10.0f),new NSString("inputRadius"));
    CIImage resultImage = (CIImage) gaussianBlurFilter.ValueForKey(new NSString("outputImage"));

    UIImage finalImage = new UIImage(resultImage, 1.0f, UIImageOrientation.Up);
    UIImageView  imageView = new UIImageView(view.Bounds);
    imageView.Image = finalImage;
}

Note: the typo has been fixed in MonoTouch and the constructor will be usable in future releases (6.0.9+)

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Thanks works and I believe it should also work on ios 5.11 but I havent tested it. but now I have it working the performance overhead dose not justify the effect it creates. many thanks – canderse Dec 09 '12 at 23:14
  • No problem. Please take the time to mark (gray/green checkmark below the votes) the question as answered so people will a similar question can quickly identify an answer is available to them (and welcome to stackoverflow :-) – poupou Dec 10 '12 at 00:23