gpt4 book ai didi

ios - 如何截取 UIView 的屏幕截图并使用它具有模糊背景?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:02:38 34 4
gpt4 key购买 nike

我有一个 View Controller ,它由一个容器 View 组成,容器 View 是 ImageView 上方屏幕大小的一半,最初是隐藏的。容器嵌入了 tableView。我有一个按钮,点击它会显示容器 View ,第二次点击它会隐藏。当容器 View 打开时,背景 View 应该是模糊的。对于那个模糊 View ,我想截取屏幕截图图像具有背景的原始 View 位置的屏幕截图并对其进行模糊处理。 这个我试过了

- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_imageView.image = [UIImage imageNamed:@"img"];
return img;
}


- (IBAction)clickToBlur:(id)sender {

[self imageWithView:_imageView];
_imageView.image = [UIImage imageNamed:@"img"];


if (!UIAccessibilityIsReduceTransparencyEnabled()) {
self.view.backgroundColor = [UIColor clearColor];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect ];
blurEffectView.frame = self.view.bounds;
blurEffectView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.6];
blurEffectView.frame = CGRectMake(0, 28, 375, 554);
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:blurEffectView];
} else {
self.view.backgroundColor = [UIColor blackColor];
}
}

enter image description here

最佳答案

制作自定义 View ,然后拍摄屏幕截图并将其固定在该 View 上,并使您的 View 背景模糊

截图代码:

- (UIImage *) screenshot {

CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

CGRect rec = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];

image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

关于ios - 如何截取 UIView 的屏幕截图并使用它具有模糊背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40909466/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com