亲宝软件园·资讯

展开

iOS实现毛玻璃效果 iOS实现毛玻璃效果(无需要第三方)

请给我一杯阳光 人气:0
想了解iOS实现毛玻璃效果(无需要第三方)的相关内容吗,请给我一杯阳光在本文为您仔细讲解iOS实现毛玻璃效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:iOS,毛玻璃,下面大家一起来学习吧。

本文实例分享两种iOS毛玻璃效果设置的方法,不需要任何第三方,先看效果:

原图:

方法一(iOS8系统方法):

方法二:

下面是示例代码:

#import "ViewController.h" 
 
@interface ViewController () 
{ 
 UIImageView *_imageView; 
} 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; 
 _imageView.image = [UIImage imageNamed:@"1.jpg"]; 
 [self.view addSubview:_imageView]; 
 
 //方法一:系统方法,iOS8及以上可用 
 if (!UIAccessibilityIsReduceTransparencyEnabled()) { 
 UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; 
 UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect]; 
 blurEffectView.frame = _imageView.bounds; 
 [_imageView addSubview:blurEffectView]; 
 } 
 
 
 //方法二:Core Image 
 UIImageView *blurImageView = [[UIImageView alloc]initWithFrame:_imageView.bounds]; 
 blurImageView.image = [self blur:[UIImage imageNamed:@"1.jpg"]]; 
 [_imageView addSubview:blurImageView]; 
} 
 
//生成一张毛玻璃图片 
- (UIImage*)blur:(UIImage*)theImage 
{ 
 CIContext *context = [CIContext contextWithOptions:nil]; 
 CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage]; 
 
 CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
 [filter setValue:inputImage forKey:kCIInputImageKey]; 
 [filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"]; 
 CIImage *result = [filter valueForKey:kCIOutputImageKey]; 
 
 CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; 
 
 UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; 
 CGImageRelease(cgImage); 
 return returnImage; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
@end 

具体效果和参数自行研究吧!

加载全部内容

相关教程
猜你喜欢
用户评论