0

我在模态 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 中时,视图控制器会立即向上滑动。

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

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

4

1 回答 1

0

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]; }
于 2014-10-23T09:25:18.210 回答