毛玻璃蒙版

应用程序毛玻璃蒙版保护账户信息(如支付宝)

先说说实现的的思路:

当程序即将进入后台时,把当前屏幕截个图,此时要将图片毛玻璃化,并作为UIImageView的image,然后将imageView放在window的最上面,等即将进入前台时移除毛玻璃蒙版。

下面就来说代码实现吧(在代理文件中实现):


1

2

3

@interface AppDelegate ()

@property (nonatomic, weak)UIImageView *cover;

@end

主要代码:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    self.cover = imgView;
    [[UIApplication sharedApplication].keyWindow addSubview:imgView];

    UIGraphicsBeginImageContext([UIApplication sharedApplication].keyWindow.size);
    // 将当前layer的截图写进上下中
    [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    // 从上下中读取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  // 压缩图片,并转成JPEG类型
    NSData *imageData = UIImageJPEGRepresentation(image, 0.01);

    image = [UIImage imageWithData:imageData];
  // 注意:不能直接将图片毛玻璃化,要转成data后,再转成image,此时的image才能毛玻璃化,不然调用blurryImage:withBlurLevel会报错。
    imgView.image = [UIImage blurryImage:image withBlurLevel:0.15];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    [self.cover removeFromSuperview];
}

//下面代码是 blurryImage:withBlurLeve方法l的具体实现,我将它写成了UIImage的分类,所以可以直接用UIImage调用。

//加模糊效果,image是图片,blur是模糊度
+ (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    //模糊度,
    if ((blur < 0.1f) || (blur > 2.0f)) {
        blur = 0.5f;
    }

    //boxSize必须大于0
    int boxSize = (int)(blur * 100);
    boxSize -= (boxSize % 2) + 1;
    NSLog(@"boxSize:%i",boxSize);
    //图像处理
    CGImageRef img = image.CGImage;
    //需要引入#import <Accelerate/Accelerate.h>
    /*
     This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing.
     本文档介绍了Accelerate Framework,其中包含C语言应用程序接口(API)的向量和矩阵数学,数字信号处理,大量处理和图像处理。
     */

    //图像缓存,输入缓存,输出缓存
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    //像素缓存
    void *pixelBuffer;

    //数据源提供者,Defines an opaque type that supplies Quartz with data.
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    // provider’s data.
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    //宽,高,字节/行,data
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    //像数缓存,字节行*图片高
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));

    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);

    // 第三个中间的缓存区,抗锯齿的效果
    void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    vImage_Buffer outBuffer2;
    outBuffer2.data = pixelBuffer2;
    outBuffer2.width = CGImageGetWidth(img);
    outBuffer2.height = CGImageGetHeight(img);
    outBuffer2.rowBytes = CGImageGetBytesPerRow(img);

    //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.
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);

    if (error) {
        NSLog(@"error from convolution %ld", error);
    }

    //    NSLog(@"字节组成部分:%zu",CGImageGetBitsPerComponent(img));
    //颜色空间DeviceRGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //用图片创建上下文,CGImageGetBitsPerComponent(img),7,8
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             CGImageGetBitmapInfo(image.CGImage));

    //根据上下文,处理过的图片,重新组件
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);

    free(pixelBuffer);
    free(pixelBuffer2);
    CFRelease(inBitmapData);

    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);

    return returnImage;
}
时间: 2024-08-29 01:54:44

毛玻璃蒙版的相关文章

应用程序加毛玻璃蒙版保护账户信息

废话不多说,先来看看没有蒙版的效果 ,再来看看有蒙版的效果  ,明显可以看出有明显的区别. 先说说实现的的思路: 当程序即将进入后台时,把当前屏幕截个图,此时要将图片毛玻璃化,并作为UIImageView的image,然后将imageView放在window的最上面,等即将进入前台时移除毛玻璃蒙版. 下面就来说代码实现吧(在代理文件中实现): @interface AppDelegate () @property (nonatomic, weak)UIImageView *cover; @end

剪贴蒙版和链接拼图

选择要拼的图片,复制一个. 新建一个图层填充背景颜色. 在图片上创建一个选区填充白色,作为边框. 再创建一个黑色选区.把三个四链接起来. 选中复制的图片1,按alt键与黑色图层4创建剪贴蒙版. 移动4图层调整合适的位置 把1-5 ctrl+g分组. 复制组,拼合图片.

iOS View 模糊效果(毛玻璃)

相关资料 http://stackoverflow.com/questions/18404907/using-gpuimage-to-recreate-ios-7-glass-effect http://stackoverflow.com/questions/17036655/ios-7-style-blur-view/17048668#17048668 我没有用GPUImge  使用了  FXBlurView which works great on iOS5+ 只有两个文件 https://

在UnrealEngine中用Custom节点实现毛玻璃的效果

本人在论坛上找到了一篇实现毛玻璃效果的文章:https://forums.unrealengine.com/showthread.php?70143-So-Blurred-glass-material-is-impossible-in-Unreal-Engine-4&highlight=SceneTextureLookup原理是通过SceneColor获得translucent物体后面的场景渲染结果,之后根据后面的场景距离进行模糊.相关参数中还增加了TempAAParamer来实现随机值.以及添

移动端UI设计越来越流行的高斯模糊(Gaussian blur)和毛玻璃效果(磨砂效果),如何使用Android RenderScript简单实现?

高斯模糊(Gaussian blur)和毛玻璃效果(亦称磨砂效果),近两年在移动端的UI设计上越来越流行,特别是iOS手机上出现的较多,iOS系统也提供了相应的API帮助开发人员分分钟实现这两个效果.而Android系统则经历了一个漫长的探索过程,对图片的处理,从Java算法到NDK方式实现等,各种摸索层出不穷. 值得欣慰的是,Google终于在API 11中引入了 RenderScript ,一个强大的图片处理框架,帮助Android开发人员专注于图片处理算法而不是API的调度工作.使用Ren

Android高斯模糊技术,实现毛玻璃效果(转)

本博客转自郭霖公众号:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650235930&idx=1&sn=e328709c41ae208a9e41ef79d38cbeed&scene=24&srcid=09104fpQDG98JcRcUB9Ec7BN#wechat_redirect http://blog.csdn.net/grp0916/article/details/50494712 Android高

HTML六边形蒙版的思路

前几天逛网页的时候偶然发现了个六边形的蒙版效果(其实是逛DNF官网看到的),今儿个突发奇想还原一个六边形 其实最开始做六边形我的思路是使用::before和::after 1 .box2{ 2 width: 400px; 3 height: 400px; 4 border: 1px solid black; 5 position: relative; 6 } 7 .six4{ 8 width: 100px; 9 height: 173px; 10 position: absolute; 11 t

仿QQ发送图片时选中后加蒙版(想看跑车请进)

主要实现给GridView加CheckBox选中后可以加蒙版 FruitAdapter 添加CheckBox和蒙版只需在FruitAdapter中完成造作即可 public class FruitAdapter extends BaseAdapter { private LayoutInflater mInflater; private List<Fruit> mFruits; private boolean mCheckBoxManager[]; public FruitAdapter(La

用swing实现了io7的毛玻璃效果

话不多说,上图 我new 了一些按钮,每个按钮的位置和背景颜色是随机的,其中有一些按钮的背景色每隔一秒变一次,拖动顶层panel,panel下的所有控件都将会模糊,而且是实时和动态的,比如下图,按钮为选择状态 panel背后的有些按钮在实时的变色,同样可以及时的模糊,模糊效果在很多应用上可以使用,模糊的背景能减弱视觉疲劳,还可以吸引用户的注意力. 源码地址:http://download.csdn.net/detail/lw421058391/7731725 用swing实现了io7的毛玻璃效果