模糊效果(毛玻璃效果)

模糊效果(毛玻璃效果)


代碼位置:https://github.com/sunyanyan/blurredImageSamplehtml

效果演示:ios

1. 使用iOS自帶的 UIImage+ImageEffects 文件

apple官方UIImage+ImageEffects文件位置git

文件中有這麼幾個方法:github

- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;

- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;

2. 使用 CoreImage 中的模糊濾鏡

  1. coreImage是蘋果用來簡化圖片處理的框架
  2. CIImage, CIFilter與CIContext三者之間的聯繫(CoreImage中三個重要的類)

示例:app

-(UIImage*)applyGaussianBlurImage:(UIImage*)image {
// CIImage
CIImage *ciImage         = [[CIImage alloc] initWithImage:image];
// CIFilter(濾鏡的名字, 不要寫錯 高斯模糊)
CIFilter *blurFilter     = [CIFilter filterWithName:@"CIGaussianBlur"];
// 將圖片輸入到濾鏡中
[blurFilter setValue:ciImage forKey:kCIInputImageKey];
/**在傳入圖片進入濾鏡後,能夠更改濾鏡的一些參數進行設置,好比模糊程度等*/
//    NSLog(@"%@", [blurFilter attributes]); // 打印看一下有哪些參數能夠設置及相關信息
// inputRadius參數: 模糊的程度 默認爲10, 範圍爲0-100, 接收的參數爲NSNumber類型
// 設置模糊的程度
[blurFilter setValue:@(8) forKey:@"inputRadius"];
// 將處理好的圖片輸出
CIImage *outImage        = [blurFilter valueForKey:kCIOutputImageKey];
// CIContext 上下文(參數nil->默認爲CPU渲染, 若是想用GPU渲染來提升效率的話,則須要傳參數)
CIContext *context       = [CIContext contextWithOptions:nil];
// 將處理好的圖片建立出來 outImage原來的大小size
CGImageRef outputCGImage = [context createCGImage:outImage
                                         fromRect:[outImage extent]];
UIImage *blurImage       = [UIImage imageWithCGImage:outputCGImage];
// 釋放CGImageRef
CGImageRelease(outputCGImage);
return blurImage;
}

3. 使用 UIVisualEffectView (實時)(iOS8)

//生成該對象
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]; //而後添加將其添加到相應的UIView 之上
相關文章
相關標籤/搜索