• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS实现高斯模糊效果(Swift版本)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

给UIimage添加分类

 1 extension UIImage {
 2     /// 高斯模糊
 3     func gaussianBlur(var blurAmount:CGFloat) -> UIImage {
 4         //高斯模糊参数(0-1)之间,超出范围强行转成0.5
 5         if (blurAmount < 0.0 || blurAmount > 1.0) {
 6             blurAmount = 0.5
 7         }
 8 
 9         var boxSize = Int(blurAmount * 40)
10         boxSize = boxSize - (boxSize % 2) + 1
11 
12         let img = self.CGImage
13 
14         var inBuffer = vImage_Buffer()
15         var outBuffer = vImage_Buffer()
16 
17         let inProvider =  CGImageGetDataProvider(img)
18         let inBitmapData =  CGDataProviderCopyData(inProvider)
19 
20         inBuffer.width = vImagePixelCount(CGImageGetWidth(img))
21         inBuffer.height = vImagePixelCount(CGImageGetHeight(img))
22         inBuffer.rowBytes = CGImageGetBytesPerRow(img)
23         inBuffer.data = UnsafeMutablePointer<Void>(CFDataGetBytePtr(inBitmapData))
24 
25         //手动申请内存
26         let pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img))
27 
28         outBuffer.width = vImagePixelCount(CGImageGetWidth(img))
29         outBuffer.height = vImagePixelCount(CGImageGetHeight(img))
30         outBuffer.rowBytes = CGImageGetBytesPerRow(img)
31         outBuffer.data = pixelBuffer
32 
33         var error = vImageBoxConvolve_ARGB8888(&inBuffer,
34             &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
35             UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
36         if (kvImageNoError != error)
37         {
38             error = vImageBoxConvolve_ARGB8888(&inBuffer,
39                 &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
40                 UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
41             if (kvImageNoError != error)
42             {
43                 error = vImageBoxConvolve_ARGB8888(&inBuffer,
44                     &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
45                     UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
46             }
47         }
48 
49         let colorSpace =  CGColorSpaceCreateDeviceRGB()
50         let ctx = CGBitmapContextCreate(outBuffer.data,
51             Int(outBuffer.width),
52             Int(outBuffer.height),
53             8,
54             outBuffer.rowBytes,
55             colorSpace,
56             CGImageAlphaInfo.PremultipliedLast.rawValue)
57 
58         let imageRef = CGBitmapContextCreateImage(ctx)
59 
60         //手动申请内存
61         free(pixelBuffer)
62         return UIImage(CGImage: imageRef!)
63     }
64 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
50天iOS挑战(Swift) - 第5天:模仿网易新闻顶端滑动分类列表发布时间:2022-07-13
下一篇:
如何把字符串数组从Swift传递给C发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap