我在模态 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 值,这似乎不会减少应用模糊所需的时间。
有什么办法可以让它跑得更快吗?