iOS 圖片添加模糊效果

iOS圖片模糊效果的兩種實(shí)現(xiàn)方法:
1.CoreImage的模糊濾鏡

UIImage * sourceImage = [UIImage imageNamed:@"imageName"];

CIImage * ciImage    = [[CIImage alloc] initWithImage:sourceImage];

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

//將圖片輸入到濾鏡中

[blurFilter setValue:ciImage forKey:kCIInputImageKey];

//設(shè)置模糊程度

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

NSLog(@"查看blurFilter的屬性--- %@",blurFilter.attributes);

//將處理之后的圖片輸出

CIImage * outCiImage    = [blurFilter valueForKey:kCIOutputImageKey];

CIContext * context      = [CIContext contextWithOptions:nil];

*//獲取CGImage句柄*

*CGImageRef outCGImageRef = [context createCGImage:outCiImage fromRect:[outCiImage extent]];*

//獲取到最終圖片

UIImage * resultImage    = [UIImage imageWithCGImage:outCGImageRef];

//釋放句柄

CGImageRelease(outCGImageRef);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * sourceImage.size.height / sourceImage.size.width)];

[imageView setImage:resultImage];

[self.view addSubview:imageView];

2.iOS系統(tǒng)自帶的高斯模糊效果(只能在iOS8以上使用)

   UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
   UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
   effectView.alpha = 0.7;
   effectView.frame = bgImg.frame;
   [bgImg addSubview:effectView];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容