Fast blur on iOS(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.
我的第一种方法是每隔 .01 秒调用一次 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.
我尝试过的模糊方法是:
The methods i've tried for blurring are:
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.
我的一个想法是重写我使用的所有组件(UITableViewCells、UITableView 等)的 drawRect
方法,并在运行中独立地对每个组件进行模糊处理.然而这需要一些时间,这听起来像是一个可行的选择吗?
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
:
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. :(
我看到某些实现只有在我传入从磁盘加载的图像时才会模糊.如果我传入使用 UIGraphicsGetImageFromCurrentImageContext()
创建的 UIImage,它将不起作用.关于为什么会这样的任何想法?
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?
更新:
我已经尝试过patel的建议如下:
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 让 BlurBox 正常工作,因为他添加了标准化即时将图像转换为 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 上的快速模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 上的快速模糊
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01