码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 毛玻璃效果

时间:2015-09-22 21:44:16      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

当用户点击按钮后,让整个屏幕产生毛玻璃效果,模糊背景,在此基础上显示一些View,提示用户操作成功!

在iOS开发中,可以给整个屏幕截个图,通过一定的算法对这张图片进行模糊,显示在一个蒙板上,并在这个蒙板上添加想要的UI效果

核心代码如下:

  1 /*** 添加蒙版 ***/
  2 - (void)addLayer{
  3 // 截屏,并添加毛玻璃效果
  4     UIGraphicsBeginImageContext(CGSizeMake(kScreenW, kScreenH));
  5     [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
  6     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  7     UIGraphicsEndImageContext();
  8     UIImage *image = [UIImage blurryImage:viewImage withBlurLevel:0.15];
  9 //添加的蒙层
 10     UIWindow *keyWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
 11     [keyWindow setWindowLevel:UIWindowLevelAlert];
 12     [keyWindow setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.8]];
 13     [keyWindow makeKeyAndVisible];
 14     self.pwdWindow = keyWindow;
 15 
 16 //将模糊后的image设置到UIImageView上展示,并可以在UIWindow上添加其他的控件
 17     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW , kScreenH)];
 18     imageView.image = image;
 19     imageView.userInteractionEnabled = YES;
 20     [self.pwdWindow addSubview:imageView];
 21 }
 22 /**
 23  * 把截屏照片传进去,进行模糊
 24  * 该方法可以作为UIImageView的分类
 25  **/
 26 + (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
 27     //模糊度,
 28     if ((blur < 0.1f) || (blur > 2.0f)) {
 29         blur = 0.5f;
 30     }
 31 
 32     //boxSize必须大于0
 33     int boxSize = (int)(blur * 100);
 34     boxSize -= (boxSize % 2) + 1;
 35     NSLog(@"boxSize:%i",boxSize);
 36     //图像处理
 37     CGImageRef img = image.CGImage;
 38     //需要引入
 39     /*
 40      This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing.
 41      本文档介绍了Accelerate Framework,其中包含C语言应用程序接口(API)的向量和矩阵数学,数字信号处理,大量处理和图像处理。
 42      */
 43 
 44     //图像缓存,输入缓存,输出缓存
 45     vImage_Buffer inBuffer, outBuffer;
 46     vImage_Error error;
 47     //像素缓存
 48     void *pixelBuffer;
 49     //数据源提供者,Defines an opaque type that supplies Quartz with data.
 50     CGDataProviderRef inProvider = CGImageGetDataProvider(img);
 51     // provider’s data.
 52     CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
 53     //宽,高,字节/行,data
 54     inBuffer.width = CGImageGetWidth(img);
 55     inBuffer.height = CGImageGetHeight(img);
 56     inBuffer.rowBytes = CGImageGetBytesPerRow(img);
 57     inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
 58     //像数缓存,字节行*图片高
 59     pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
 60     outBuffer.data = pixelBuffer;
 61     outBuffer.width = CGImageGetWidth(img);
 62     outBuffer.height = CGImageGetHeight(img);
 63     outBuffer.rowBytes = CGImageGetBytesPerRow(img);
 64     // 第三个中间的缓存区,抗锯齿的效果
 65     void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
 66     vImage_Buffer outBuffer2;
 67     outBuffer2.data = pixelBuffer2;
 68     outBuffer2.width = CGImageGetWidth(img);
 69     outBuffer2.height = CGImageGetHeight(img);
 70     outBuffer2.rowBytes = CGImageGetBytesPerRow(img);
 71 
 72     //Convolves a region of interest within an ARGB8888 source image by an implicit M x N kernel that has the effect of a box filter.
 73     error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
 74     //    error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
 75     error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
 76     if (error) {
 77         NSLog(@"error from convolution %ld", error);
 78     }
 79     //    NSLog(@"字节组成部分:%zu",CGImageGetBitsPerComponent(img));
 80     //颜色空间DeviceRGB
 81     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
 82     //用图片创建上下文,CGImageGetBitsPerComponent(img),7,8
 83     CGContextRef ctx = CGBitmapContextCreate(
 84                                              outBuffer.data,
 85                                              outBuffer.width,
 86                                              outBuffer.height,
 87                                              8,
 88                                              outBuffer.rowBytes,
 89                                              colorSpace,
 90                                              CGImageGetBitmapInfo(image.CGImage));
 91     //根据上下文,处理过的图片,重新组件
 92     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
 93     UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
 94     //clean up
 95     CGContextRelease(ctx);
 96     CGColorSpaceRelease(colorSpace);
 97     free(pixelBuffer);
 98     free(pixelBuffer2);
 99     CFRelease(inBitmapData);
100     CGColorSpaceRelease(colorSpace);
101     CGImageRelease(imageRef);
102     return returnImage;
103 }

效果图:

 

技术分享

iOS 毛玻璃效果

标签:

原文地址:http://www.cnblogs.com/caofutao/p/4830444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!