Ios 模式VC和旋转的自定义背景
我在iPad上为模态视图控制器实现了一个自定义(模糊)背景。我是这样做的:在演示VC的Ios 模式VC和旋转的自定义背景,ios,objective-c,ios7,uiviewcontroller,Ios,Objective C,Ios7,Uiviewcontroller,我在iPad上为模态视图控制器实现了一个自定义(模糊)背景。我是这样做的:在演示VC的视图中,将显示:我拍摄演示VC视图的快照图像,模糊它,并在演示VC的顶部添加UIImage视图。这很有效 但是,当我在显示模式时旋转设备时,模糊图像与它所表示的视图控制器不同步。图像会拉伸以填充屏幕,但视图控制器不会以相同的方式调整其视图的大小。因此,当模态消失,模糊消失时,过渡看起来很糟糕 我曾尝试在didRotateFromInterfaceOrientation:中拍摄另一张快照,并替换模糊图像,但没有成
视图中,将显示:
我拍摄演示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];
}
}
Utils代码
+ (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;
}
我编写了一个简单的示例,它从
presentingViewController
获取一个屏幕截图,并将其设置为背景图像。下面是我的模态视图控制器的实现。而且它工作得很好(在视图上都会出现并正确处理旋转)。你能不能也发STSUtils
code
#import "ScreenshoterViewController.h"
@interface ScreenshoterViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)buttonBackPressed;
@end
@implementation ScreenshoterViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateImage];
}
- (void)updateImage
{
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.presentingViewController.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = img;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self updateImage];
}
- (IBAction)buttonBackPressed {
[self dismissViewControllerAnimated:YES
completion:NULL];
}
@end
更新
+ (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;
}
在您的utils代码中,只需替换此
[view drawViewHierarchyInRect:frame afterScreenUpdates:NO];
用这个
[view drawViewHierarchyInRect:frame afterScreenUpdates:YES];
来自苹果文档:
AFTER更新一个布尔值,该值指示快照
应在合并最近的更改后进行渲染。
如果要在视图中渲染快照,请指定值“否”
层次结构的当前状态,可能不包括最近的更改
因此,您拍摄的屏幕截图中没有包含视图中的最新更改。请发布实际代码。@MaxK发布了代码。为什么要将子视图添加到self.presentingViewController
?如果我理解正确,您希望在模式视图中显示presentingViewController
的模糊图像,那么您应该执行[self.view addSubView:\u backgroundBlurView]
@MaxK我只想更改iPad上模式(非全屏)视图的默认外观。默认行为是使背景半透明。我给它加了一点模糊。模态视图控制器不受影响。模糊图像需要在模式下,所以我将其添加到呈现VC。我希望这是有意义的!请用以下代码将图像保存到照片中:UIImage*img=[STSUtils imageByBlurringView:self.presentingViewController.view];UIImageWriteToSavedPhotosAlbum(img,nil,nil,NULL)代码>并在这里发布。我们需要定位一个错误——不管它是截屏还是显示错误。尝试使用带和不带演示模式控制器的两种定向。我想你在这里回答了一个稍微不同的问题:)我不想快照演示者VC并在模式VC中显示它。我希望用自己的快照图像覆盖presenter VC,但模糊。我将尝试绘制一个图表并将其添加到我的问题中。对不起,第一次没有得到您想要的。但现在,我会尽力帮助你。我已经发布了一个示例项目来说明这个问题。尝试(1)打开模式,然后(2)旋转屏幕,然后(3)关闭模式。你会发现(2)中模糊的背景与(3)中的实际背景不匹配。为什么这是公认的答案?是否有一个示例项目显示了重新绘制的工作?我将用viewWillLayoutSubviews替换didRotateFromInterfaceOrientation-效果更好,但仍然无法以最佳方式处理旋转。