IOS 毛玻璃 效果實現

來源:互聯網
上載者:User

標籤:des   color   io   os   ar   for   資料   sp   art   

//加模糊效果,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;

}


IOS 毛玻璃 效果實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.