iOS中的模糊效果

iOS中的模糊效果

App设计时往往会用到一些模糊效果。iOS目前已提供一些模糊API可以让我们方便是使用。一种是使用Core Image,另一种是使用Accelerate.Framework中的vImage API。

使用Core Image进行模糊

Core Image很早在Mac系统中得到应用,后来这个Framework也开始应用到iOS,不过直到iOS6.0才开始支持模糊。这个API调用起来很方便简洁。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (UIImage *)blurryImage:(UIImage *)image
            withBlurLevel:(CGFloat)blur {
     CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
     CIFilter *filter = [CIFilter filterWithName: @"CIGaussianBlur"
                          keysAndValues:kCIInputImageKey, inputImage,
                                        @"inputRadius" , @(blur),
                                        nil ];
     
     CIImage *outputImage = filter.outputImage;
     
     CGImageRef outImage = [ self .context createCGImage:outputImage
                                    fromRect:[outputImage extent]];
     return [UIImage imageWithCGImage:outImage];
}
使用vImage API进行模糊

iOS5.0中新增了vImage API可以使用,它属于Accelerate.Framework,所以如果你要使用它要在工程中加入这个Framework。模糊算法使用的是vImageBoxConvolve_ARGB8888这个函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
     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;
}
  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值