【发布时间】:2014-03-24 18:44:34
【问题描述】:
我为 iPad 上的模态视图控制器实现了自定义(模糊)背景。我是这样做的:在展示的 VC 的viewWillShow: 中,我拍摄展示的 VC 视图的快照图像,将其模糊,然后在展示的 VC 顶部添加一个 UIImage 视图。这很好用。
但是,当我在显示模式时旋转设备时,模糊图像与它所代表的视图控制器不同步。图像被拉伸以填满屏幕,但视图控制器不会以相同的方式调整其视图的大小。因此,当模态消失并且模糊消失时,过渡看起来很糟糕。
我尝试在didRotateFromInterfaceOrientation: 中拍摄另一张快照,并替换模糊的图像,但没有奏效。
关于如何解决这个问题的任何建议?
我的代码(在模态视图控制器中):
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self drawBackgroundBlurView];
}
- (void)drawBackgroundBlurView
{
if(_blurModalBackground) {
[_backgroundBlurView removeFromSuperview];
CGRect backgroundFrame = self.presentingViewController.view.bounds;
_backgroundBlurView = [[UIImageView alloc] initWithFrame:backgroundFrame];
_backgroundBlurView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_backgroundBlurView setImage:[STSUtils imageByBlurringView:self.presentingViewController.view]];
[self.presentingViewController.view addSubview:_backgroundBlurView];
}
}
实用程序代码
+ (UIImage *)imageByBlurringView:(UIView *)view
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
BOOL isPortrait = (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]));
CGRect frame;
if(isPortrait) {
frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.height, view.frame.size.width);
}
else {
frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
}
UIGraphicsBeginImageContextWithOptions(frame.size, NO, window.screen.scale);
[view drawViewHierarchyInRect:frame afterScreenUpdates:NO];
// Get the snapshot
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
// Apply blur
UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3];
UIImage *blurredSnapshotImage = [snapshotImage applyBlurWithRadius:8
tintColor:tintColor
saturationDeltaFactor:1.8
maskImage:nil];
UIGraphicsEndImageContext();
return blurredSnapshotImage;
}
【问题讨论】:
-
能否请您发布实际代码。
-
@MaxK 发布了代码。
-
为什么要给
self.presentingViewController添加子视图?如果我理解正确,您想在模态图中显示presentingViewController的模糊图像 - 那么您应该这样做[self.view addSubView:_backgroundBlurView] -
@MaxK 我只想更改 iPad 上模态(非全屏)视图的默认外观。默认行为是使背景半透明。我为此添加了一个模糊。模态视图控制器不受影响。模糊图像需要位于模态下方,因此我将其添加到呈现的 VC 中。我希望这是有道理的!
-
能否请您将图片保存成照片并使用以下代码
UIImage *img = [STSUtils imageByBlurringView:self.presentingViewController.view]; UIImageWriteToSavedPhotosAlbum(img, nil, nil, NULL);并在此处发布。我们需要定位一个错误——无论是截屏还是显示不正确。尝试使用和不使用模态控制器的两种方向。
标签: ios objective-c ios7 uiviewcontroller