简体   繁体   中英

How to do a motion blur effect on an UIImageView in Monotouch?

What's the way to do a realtime motion blur in MonoTouch?

I need to apply a motion blur effect on an UIImageView when scrolling an inertial picture gallery, with strength and direction as parameters, as in Photoshop.

I can't find any blur or motion blur filter in CocoaTouch or in CoreAnimation or anywhere in the iOS SDK.

Is there some 2D effects library usable from MonoTouch with realtime blur filters that are good for the iPhone?

Thanks in advance.

I've found a good open source library to do many effects on UIImage:

https://github.com/gdawg/uiimage-dsp

It also uses the Accelerated framework to speed up the things a little on iOS 4.0.

Now, if only there was a MonoTouch wrapper of this...

If you want to use the blur Apple demoed at WWDC (although not realtime!) I made a native port of their UIImage categories.

Getting the blur is a little tedious, but doable:

// Helper method to create an image snapshot of the view hierarchy
UIImage SnapshotImageWithScale (UIView view, float scale)
{
    UIGraphics.BeginImageContextWithOptions (view.Bounds.Size, false, scale);
    view.DrawViewHierarchy (view.Bounds, true);

    UIImage image = UIGraphics.GetImageFromCurrentImageContext ();
    UIGraphics.EndImageContext ();

    return image;
}

...

// Create snapshot of the parent view controller (this can be the superview or anything else)
UIImage snapshot = SnapshotImageWithScale (parentView, UIScreen.MainScreen.Scale);

// Blur the snapshot with the specified radius and tint color
snapshot = snapshot.ApplyBlur (6.0f, UIColor.FromWhiteAlpha (0.0f, 0.6f), 0.8f, null);

// Create an UIImageView to display the blurred snapshot
UIImageView snapshotView = new UIImageView {
    Frame = new RectangleF (0, 0, View.Bounds.Width, View.Bounds.Height),
    Image = snapshot,
};
View.AddSubview (snapshotView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM