gpt4 book ai didi

ios - 如何在 iOS 的 UIView 上应用模糊效果?

转载 作者:技术小花猫 更新时间:2023-10-29 10:42:19 31 4
gpt4 key购买 nike

在我的应用程序中,我想在 uiview 上应用模糊效果。那么如何实现模糊效果。我尝试了以下代码:

UIGraphicsBeginImageContext(scrollview.bounds.size);
[scrollview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the UIImage with a CIFilter
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:3] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:scrollview.bounds];
newView.image = endImage;
[scrollview addSubview:newView];

但使用此代码时遇到问题。当应用模糊效果时 View 变小。

最佳答案

只需将此模糊 View 放在您想要模糊的 View (这里是您的BlurredView)上。这是 Objective-C 中的示例:

UIVisualEffect *blurEffect; 
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

visualEffectView.frame = yourBlurredView.bounds;
[yourBlurredView addSubview:visualEffectView];

和 swift :

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))    

visualEffectView.frame = yourBlurredView.bounds

yourBlurredView.addSubview(visualEffectView)

关于ios - 如何在 iOS 的 UIView 上应用模糊效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366199/

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