在iOS上快速模糊 [英] Fast blur on iOS

查看:121
本文介绍了在iOS上快速模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试模糊iOS应用中的部分屏幕时遇到了麻烦。查看图片以更好地了解我正在尝试做什么。

I'm running into trouble trying to blur part of the screen in my iOS app. See image for better idea of what I'm trying to do.

只有BlurBox的内容需要模糊,但其余内容可以清晰。因此,如果您正在查看表格视图,则只有BlurBox下面的内容会模糊(即使您滚动)。其余的看起来很清楚。

Only the content of the "BlurBox" needs to be blurry but the rest can be clear. So if you were looking at table view, only the content underneath the BlurBox would be blurry (even as you scroll). The rest would look clear.

我的第一种方法是每隔.01s调用 UIGraphicsGetImageFromCurrentImageContext()来获取BlurBox下的所有图层一个图像。然后模糊该图像并将其显示在所有图像上。

My first approach was to call UIGraphicsGetImageFromCurrentImageContext() every .01s to get all the layers under the BlurBox mushed into one image. Then blur that image and display it onto of everything.

我试图模糊的方法是:

https://github.com/tomsoft1/StackBluriOS

https://github.com/coryleach/UIImageAdjust

https://github.com/esilverberg/ios-image-filters

https://github.com/cmkilger/CKImageAdditions

[layer setRasterizationScale:0.25];
[layer setShouldRasterize:YES];

以及一些自定义尝试。我还看了Apple的 GLImageProcessing 但是我认为我在这里尝试做的事情有点矫枉过正。

As well as a few custom attempts. I've also looked at Apple's GLImageProcessing but I think that it is a bit overkill for what I'm trying to do here.

问题在于它们都 。该应用程序不在应用程序商店上,所以我愿意使用任何私有/未记录的框架。

The problem is that they are all to slow. The app is not going on the app store so I'm open to using any private/undocumented frameworks.

我有一种很棒的想法是覆盖我使用的所有组件的 drawRect 方法(UITableViewCells ,UITableView等)并在运行中独立模糊它们。然而,这需要一些时间,这听起来像是一个可行的选择吗?

A kind of far out idea I had was to override the drawRect method of all the components I use (UITableViewCells, UITableView, etc) and blur each of them independently on the fly. However this would take some time, does this even sound like a viable option?

更新

我尝试使用 CIFilters ,如下所示:

I have tried to use CIFilters as follows:

CIImage *inputImage = [[CIImage alloc] initWithImage:[self screenshot]];

CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
[blurFilter setValue: inputImage forKey: @"inputImage"];
[blurFilter setValue: [NSNumber numberWithFloat:10.0f]
                           forKey:@"inputRadius"];


CIImage *outputImage = [blurFilter valueForKey: @"outputImage"];

CIContext *context = [CIContext contextWithOptions:nil];

self.bluredImageView.image = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];

这确实有效,但速度非常慢。 :(

This does work, however it is incredibly slow. :(

我看到一些实现只有在我传入从磁盘加载的图像时才会模糊。如果我传入一个我使用<$ c创建的UIImage $ c> UIGraphicsGetImageFromCurrentImageContext()它不起作用。有关为什么会这样的想法?

I am seeing that some implementations will blur only when I pass in an image loaded from disk. If I pass in a UIImage that I created from using UIGraphicsGetImageFromCurrentImageContext() it doesn't work. Any ideas on why this would be?

更新

我已尝试过如下的髌骨建议:

I have tried patel's suggestion as follows:

CALayer *backgroundLayer = [CALayer layer];

CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
backgroundLayer.backgroundFilters = [NSArray arrayWithObject:blurFilter];

[[self.view layer] addSublayer:backgroundLayer];

然而,它不起作用:(

更新后的更新:

我设法使用 TomSoft1的stackblur ,因为他增加了将图像标准化为RGBA格式(32位/像素)的能力。但是,它仍然很慢。

I have managed to get the BlurBox working correctly using TomSoft1's stackblur since he added the ability to normalize an image to RGBA format (32 bits/pixel) on the fly. However, it is still pretty slow.

我有一个计时器每0.03秒调用一次更新,以获取BlurBox下面的图像,模糊该图像,并在屏幕上显示。我需要帮助提高BlurBox上的fps。

I have a timer calling an update every 0.03s to grab the image of what's underneath the BlurBox, blur that image, and display it on screen. I need help on boosting the "fps" on the BlurBox.

推荐答案

我建议 Brad Larson 的GPUImage完全由GPU提供支持,可用于各种图像处理效果。它非常快,甚至足够快,在他的演示应用程序中,他可以从相机进行实时视频处理,并且帧速率非常好。

I would recommend Brad Larson's GPUImage which is fully backed by the GPU for a wide variety of image processing effects. It's very fast, and even fast enough that in his demo app he does real-time video processing from the camera and the frame-rate is excellent.

https://github.com/BradLarson/GPUImage

这是我编写的代码片段,用于应用基本的框模糊,模糊图像的底部和顶部三分之一,但图像的中间部分不模糊。他的图书馆非常广泛,几乎包含了可以想象的各种图像滤镜效果。

Here is a code snippet I wrote to apply a basic box blur which blurs the bottom and top thirds of the image but leaves the middle of the image un-blurred. His library is extremely extensive and contains almost every kind of image filter effect imaginable.

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:[self screenshot]];

GPUImageTiltShiftFilter *boxBlur = [[GPUImageTiltShiftFilter alloc] init];
        boxBlur.blurSize = 0.5;

[stillImageSource addTarget:boxBlur];

[stillImageSource processImage];

UIImage *processedImage = [stillImageSource imageFromCurrentlyProcessedOutput];

这篇关于在iOS上快速模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆