IOS 图片模糊处理 ------ 直接代码 复制出去就可用 值得标记

时间:2023-12-28 08:46:26

1.

UIImage *imag = [UIImage imageNamed:@"img"];

/* --------------------使用 coreImg  ---------------------------  */

// CIImage

CIImage *ciimg = [[CIImage alloc] initWithImage:imag];

// CIFilter

CIFilter *blurFilter =     [CIFilter filterWithName:@"CIGaussianBlur"];

// 可以输出设置的模糊的程度

NSLog(@"%@",[blurFilter attributes]);

// 根据上一步可以设置模糊程度

[blurFilter setValue:@(50) forKey:@"inputRadius"];

// 图片输入滤镜

[blurFilter setValue:ciimg forKey:kCIInputImageKey];

// 将推片输出

CIImage *outPutImg = [blurFilter valueForKey:kCIOutputImageKey];

// CIContext

CIContext * context = [CIContext contextWithOptions:nil];

// 获取CGImage句柄

CGImageRef outImgRef = [context createCGImage:outPutImg fromRect:[outPutImg extent]];

//  h获取到最后的图片

UIImage *blurImg = [UIImage imageWithCGImage:outImgRef];

// 释放句柄

CGImageRelease(outImgRef);

// 最后得到模糊的图片    blurImg

第二种

/* ___________________ UIImage+ImageEffects.h ________________________________ */

UIImage *sourImg = [UIImage imageNamed:@"img"];

UIImage *blurImg = [sourImg blurImageWithRadius:30];

第三种                    UIVisualEffectView

UIScrollView *sc = [[UIScrollView alloc] initWithFrame:self.view.bounds];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 600, 500)];

imgView.image = imag;

[sc addSubview:imgView];

sc.contentSize = imgView.bounds.size;

[self.view addSubview:sc];

UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];

effectView.frame = CGRectMake(0, 200, 400, 200);

[self.view addSubview:effectView];

UILabel *lable = [[UILabel alloc] initWithFrame:effectView.bounds];

lable.text = @"模糊背景";

lable.font = [UIFont systemFontOfSize:50];

lable.textAlignment = NSTextAlignmentCenter;

//  添加模糊的子effictView  使用的是  --  UIVibrancyEffect  且需要和父类保持一致

UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];

subEffectView.frame = effectView.bounds;

// 将子模糊View 添加到EffectView 的contenctView 才能有效果

[effectView.contentView addSubview:subEffectView];

[subEffectView.contentView addSubview:lable];