ios实现磨玻璃在各个系统上都有所不同,新系统总会有比旧系统更好的方式去实现毛玻璃
iOS 6及以下系统,可以生成一张模糊效果的图片实现
//生成一张毛玻璃图片
- (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:6.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;
}
iOS 7可以用uitoolbar实现毛玻璃效果,代码如下:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:self.view.bounds];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar setBackgroundImage:self.image.image forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefaultPrompt];
[self.view addSubview:toolBar];
然而iOS 8有了更好的api UIVisualEffectView
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = self.image.bounds;
visualEffectView.alpha = 0.7;//模糊程度
[self.image addSubview:visualEffectView];