//第一種方式是使用UIVisualEffectView實(shí)現(xiàn)
????self.imageView = [[UIImageView alloc] init];
? ? self.imageView.image = [UIImage imageNamed:@"1.jpg"];
? ? self.imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
? ? [self.view addSubview:self.imageView];
? ? //樣式可以選擇
? ? UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
? ? UIVisualEffectView *effectV = [[UIVisualEffectView alloc] initWithEffect:blur];
? ? effectV.alpha = 0.7;
? ? effectV.frame = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
? ? [self.imageView addSubview:effectV];
//第二種方式使用CoreImage實(shí)現(xiàn)
? ? //Core Image設(shè)置模糊之后會(huì)在周圍產(chǎn)生白邊
? ? UIImage *image = [UIImage imageNamed:@"1.jpg"];
? ? UIImage *blurImage = [self coreBlueImage:image];
? ? self.imageView.image = blurImage;
? ? //返回當(dāng)前類別下的所有濾鏡,更換不同的category會(huì)打印不同的濾鏡名稱
? ? /*
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryDistortionEffect;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryGeometryAdjustment;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryCompositeOperation;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryHalftoneEffect;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryColorAdjustment;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryColorEffect;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryTransition;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryTileEffect;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryGenerator;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryReduction NS_AVAILABLE(10_5, 5_0);
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryGradient;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryStylize;
? ? CORE_IMAGE_EXPORT NSString * const kCICategorySharpen;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryBlur;? ? 各種模糊分類
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryVideo;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryStillImage;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryInterlaced;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryNonSquarePixels;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryHighDynamicRange;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryBuiltIn;
? ? CORE_IMAGE_EXPORT NSString * const kCICategoryFilterGenerator NS_AVAILABLE(10_5, 9_0);
? ? */
? ? NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBlur];
? ? for (NSString *filterName in filters) {
? ? ? ? //我們可以通過filterName創(chuàng)建對(duì)應(yīng)的濾鏡對(duì)象
? ? ? ? NSLog(@"濾鏡名稱:%@",filterName);
//? ? ? ? CIFilter *filter = [CIFilter filterWithName:filterName];
//? ? ? ? NSDictionary *attributes = [filter attributes];
//? ? ? ? //獲取屬性鍵/值對(duì)(在這個(gè)字典中我們可以看到濾鏡的屬性以及對(duì)應(yīng)的key)
//? ? ? ? NSLog(@"filter attributes:%@",attributes);
? ? }
? ? /*
? ? kCICategoryBlur分類下所有濾鏡名稱
? ? CIBokehBlur
? ? CIBoxBlur
? ? CIDepthBlurEffect
? ? CIDiscBlur
? ? CIGaussianBlur
? ? CIMaskedVariableBlur
? ? CIMedianFilter
? ? CIMorphologyGradient
? ? CIMorphologyMaximum
? ? CIMorphologyMinimum
? ? CIMotionBlur
? ? CINoiseReduction
? ? CIZoomBlur
? ? */
使用到如下的方法
- (UIImage *)coreBlueImage:(UIImage *)image {
? ? //創(chuàng)建輸入CIImage對(duì)象
? ? CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
? ? //創(chuàng)建濾鏡(可以更換不同的濾鏡,濾鏡名稱可以從以上代碼獲取
? ? CIFilter *filter = [CIFilter filterWithName:@"CIBoxBlur"];
? ? //設(shè)置濾鏡屬性值為默認(rèn)值
? ? [filter setDefaults];
? ? //設(shè)置輸入圖像
? ? [filter setValue:inputImage forKey:kCIInputImageKey];
? ? //獲取輸出圖像
? ? CIImage *result = [filter valueForKey:kCIOutputImageKey];
? ? //創(chuàng)建CIContex上下文對(duì)象
? ? CIContext *context = [CIContext contextWithOptions:nil];
? ? CGImageRef outImage = [context createCGImage: result fromRect:[result extent]];
? ? UIImage *blurImage = [UIImage imageWithCGImage:outImage];
? ? CGImageRelease(outImage);
? ? return blurImage;
}
代碼已上傳至GitHub
歡迎star