【问题标题】:How To Speed Up Image Blur?如何加快图像模糊?
【发布时间】:2014-12-18 14:06:23
【问题描述】:

我在模态 uiviewcontroller 的 viewWillAppear 中有以下代码。

我在这个例子中包含了 UIImage+ImageEffects.h 来做背景图像的模糊处理。

-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:NO];

// grab an image of our parent view
UIView *parentView = self.presentingViewController.view;

UIImage *parentViewImage = [self takeSnapshotOfView:parentView];

UIImage *blurredImage = nil;

//BLUR THE IMAGE
blurredImage = [self blurWithImageEffects:parentViewImage];

// insert an image view with a picture of the parent view at the back of our view's subview stack...
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = blurredImage;
[self.view insertSubview:imageView atIndex:0];

}

[编辑] 添加模糊方法

- (UIImage *)takeSnapshotOfView:(UIView *)view
{
    CGFloat reductionFactor = 1.5;
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (UIImage *)blurWithImageEffects:(UIImage *)image
{
    return [image applyBlurWithRadius:10 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.5 maskImage:nil];
}

代码工作正常,背景显示模糊,但在装有 iOS 8 的 iPad 3 上确实明显慢。当点击显示此视图控制器的按钮时,在视图控制器从底部向上滑动之前会有一个暂停。如果我移除模糊,视图控制器会更快地向上滑动。

我尝试将代码放入 viewDidAppear 中,但随后会出现明显的白色背景,持续几秒钟后出现模糊。但是当在 viewDidAppear 中时,viewcontroller 会立即向上滑动。

如果我减小 applyWithBlurRadius 值,这似乎不会减少应用模糊所需的时间。

有什么办法可以让它跑得更快?

【问题讨论】:

  • 你可以试试 iOS 8 设备的 UIVisualEffect。
  • @skorulls 我会,但我似乎没有使用 UIVisualEffects 进行相同级别的控制,因为只有 3 级模糊可用,我似乎无法更改。我已经用图像模糊方法更新了我的问题。

标签: objective-c ipad uiimage ios8 uiblureffect


【解决方案1】:

GPUImage 可能会解决您的问题,它速度很快,并且不会对处理造成太多负担。

框架链接:https://github.com/BradLarson/GPUImage

使用 GPUImage Blur 的链接

但是下面显示的参考资料将为您提供基本概念:

首先准备你要模糊的视图:

- (NSData *)PhotoForBlurring:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:@"aName.png" atomically:YES];
    return data; }

-(void)sharingScrapPage{
     GPUImageiOSBlurFilter *blur = [[GPUImageiOSBlurFilter alloc]init];
     blur.blurRadiusInPixels=2.0;
     blur.downsampling=6.0;

     [blurview removeFromSuperview];
     blurview = [[UIImageView alloc]initWithFrame:getRectDisplay(0, 0, 480, 320)]; // Landscape Iphone 4S
     UIImage *imageForBlurring=[UIImage imageWithData:[self PhotoForBlurring:self.view]];
     NSData* pngdataForBlurring = UIImagePNGRepresentation (imageForBlurring);
     UIImage* blurImage = [UIImage imageWithData:pngdataForBlurring];
     blurImage=[blur imageByFilteringImage:blurImage];
     blurview.image=blurImage;
     [self.view addSubview:blurview];

     [progressHUD removeFromSuperview];
     progressHUD = [[ProgressHUD alloc]initWithFrame:getRectDisplay(190, 110, 100, 90)];
     [blurview addSubview:progressHUD]; }

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 2010-12-08
    • 1970-01-01
    • 2015-01-18
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多