zoukankan      html  css  js  c++  java
  • iOS_UIImage_毛玻璃效果

    效果图: 

    核心方法: 

    // 出入UIImage  和  blur模糊成度 (0-1)
    - (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
        
        /** 注意点:
         1. 需要引入#import <Accelerate/Accelerate.h>
         2. blur 的范围必须在0-1中间,否则强制为0.5
         3.
         */
        
        if (blur < 0.f || blur > 1.f) {
            blur = 0.5f;
        }
        int boxSize = (int)(blur * 100);
        boxSize = boxSize - (boxSize % 2) + 1;
        CGImageRef img = image.CGImage;
        vImage_Buffer inBuffer, outBuffer;
        vImage_Error error;
        void *pixelBuffer;
        CGDataProviderRef inProvider = CGImageGetDataProvider(img);
        CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
        inBuffer.width = CGImageGetWidth(img);
        inBuffer.height = CGImageGetHeight(img);
        inBuffer.rowBytes = CGImageGetBytesPerRow(img);
        inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
        pixelBuffer = malloc(CGImageGetBytesPerRow(img) *
                             CGImageGetHeight(img));
        if(pixelBuffer == NULL)
            NSLog(@"No pixelbuffer");
        outBuffer.data = pixelBuffer;
        outBuffer.width = CGImageGetWidth(img);
        outBuffer.height = CGImageGetHeight(img);
        outBuffer.rowBytes = CGImageGetBytesPerRow(img);
        error = vImageBoxConvolve_ARGB8888(&inBuffer,
                                           &outBuffer,
                                           NULL,
                                           0,
                                           0,
                                           boxSize,
                                           boxSize,
                                           NULL,
                                           kvImageEdgeExtend);
        if (error) {
            NSLog(@"error from convolution %ld", error);
        }
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef ctx = CGBitmapContextCreate(
                                                 outBuffer.data,
                                                 outBuffer.width,
                                                 outBuffer.height,
                                                 8,
                                                 outBuffer.rowBytes,
                                                 colorSpace,
                                                 kCGImageAlphaNoneSkipLast);
        CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
        UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
        //clean up
        CGContextRelease(ctx);
        CGColorSpaceRelease(colorSpace);
        free(pixelBuffer);
        CFRelease(inBitmapData);
        CGColorSpaceRelease(colorSpace);
        CGImageRelease(imageRef);
        return returnImage;
    }
  • 相关阅读:
    Js注释和对象
    卸载Oracle
    数据库设计三大范式
    Oracle用户管理
    Oracle权限管理详解
    Linux 性能监控之CPU&内存&I/O监控Shell脚本2
    Linux 性能监控之CPU&内存&I/O监控Shell脚本1
    Windows Win7建立wifi热点,手机共享WIFI上网
    Mariadb MySQL逻辑条件判断相关语句、函数使用举例介绍
    Mariadb MySQL、Mariadb中GROUP_CONCAT函数使用介绍
  • 原文地址:https://www.cnblogs.com/mancong/p/6140872.html
Copyright © 2011-2022 走看看