本文介绍: DispatchQueue.global().async {[weak self] in let size = CGSize.init(width: MKScreenU.width, height: (self?.headerImageViewDefaultHeight)!) guard let image = img else { return } let sc.
1.swift
1.2 代码效果图
-
模糊前
-
模糊后
1.2 代码
extension UIView {
//高斯模糊
func blurEffect(){
let blur = UIBlurEffect(style: .light)
let effectView = UIVisualEffectView(effect: blur)
effectView.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
addSubview(effectView)
}
}
imageView.blurEffect()
2.oc
2.1 代码运行后的效果图
2.2 代码
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageview = [[UIImageView alloc] init];
imageview.frame = CGRectMake(10, 100, 300, 300);
imageview.image = [UIImage imageNamed:@"img"];
imageview.contentMode = UIViewContentModeScaleAspectFit;
imageview.userInteractionEnabled = YES;
[self.view addSubview:imageview];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
effectview.frame = CGRectMake(0, 0, imageview.frame.size.width/2, 300);
[imageview addSubview:effectview];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(10, 50, 100, 40);
[btn setTitle:@"btn" forState:UIControlStateNormal];
[effectview.contentView addSubview:btn];
}
参考博客
其他:我朋友的代码
DispatchQueue.global().async {[weak self] in
let size = CGSize.init(width: MKScreenU.width, height: (self?.headerImageViewDefaultHeight)!)
guard let image = img else { return }
let scaleDownSize = CGSize.init(width: image.size.width / 10.0, height: image.size.height / 10.0)
guard let originalImage = image.sd_resizedImage(with: scaleDownSize, scaleMode: .aspectFill) else { return }
UIGraphicsBeginImageContextWithOptions(size, true, UIScreen.main.scale)
// 高斯模糊
let gimage2 = originalImage.qh_applyGaussianBlurFilter(val: 5)
let scaleValue = image.size.width / size.width
gimage2?.draw(in: CGRect.init(x: 0, y: (size.height - (image.size.height / scaleValue)) / 2.0, width: size.width, height: image.size.height / scaleValue))
// 黑色遮罩
let path = UIBezierPath.init(rect: CGRect.init(origin: .zero, size: size))
UIColor.black.withAlphaComponent(0.4).setFill()
path.fill()
let resultImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
SDImageCache.shared.store(resultImage, forKey: cacheKey) {
}
DispatchQueue.main.async {
self?.headerImageView.image = resultImage
}
}
func qh_applyGaussianBlurFilter(val: CGFloat) -> UIImage? {
guard let aCIImage = CIImage.init(image: self) else { return nil }
let clampFilter = CIFilter(name: "CIAffineClamp")
clampFilter?.setDefaults()
clampFilter?.setValue(aCIImage, forKey: kCIInputImageKey)
let blurFilter = CIFilter(name: "CIGaussianBlur")
blurFilter?.setValue(clampFilter?.outputImage, forKey: kCIInputImageKey)
blurFilter?.setValue(val, forKey: kCIInputRadiusKey)
let rect = aCIImage.extent
if let output = blurFilter?.outputImage {
let context = CIContext(options: nil)
if let cgimg = context.createCGImage(output, from: rect) {
let processedImage = UIImage(cgImage: cgimg)
return processedImage
}
}
fatalError()
}
原文地址:https://blog.csdn.net/baidu_40537062/article/details/122323195
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_25464.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。