Swift - 3.0 高斯模糊 - UIImage的扩展
//blurRadius 模糊半径 模糊覆盖颜色 maskImage 需要模糊的图片
func BAAlert_ApplyBlurWithRadius(blurRadius : CGFloat, tintColor : UIColor?, saturationDeltaFactor : Double, maskImage : UIImage?) -> UIImage! {
// Check pre-conditions.
if (self.size.width < 1 || self.size.height < 1)
{
print("*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 1: %@", self.size.width, self.size.height, self)
return nil
}
if ( self.cgImage == nil )
{
print("*** error: image must be backed by a CGImage: %@", self)
return nil
}
if ( maskImage != nil && (maskImage!.cgImage == nil) )
{
print("*** error: maskImage must be backed by a CGImage: %@", maskImage ?? <#default value#>)
return nil
}
let imageRect = CGRect(origin: CGPoint.zero, size: self.size)
var effectImage : UIImage = self
let hasBlur : Bool = blurRadius > .ulpOfOne
let hasSaturationChange : Bool = fabs(Float(saturationDeltaFactor) - 1.0) > .ulpOfOne
let screenScale = UIScreen.main.scale
if ( hasBlur || hasSaturationChange ) {
UIGraphicsBeginImageContextWithOptions(self.size, false, screenScale)
let effectInContext : CGContext = UIGraphicsGetCurrentContext()!
effectInContext.scaleBy(x: 1.0, y: -1.0)
effectInContext.translateBy(x: 0, y: -self.size.height)
effectInContext.draw(self.cgImage!, in: imageRect)
var effectInBuffer = vImage_Buffer()
effectInBuffer.data = effectInContext.data
effectInBuffer.width = vImagePixelCount(effectInContext.width)
effectInBuffer.height = vImagePixelCount(effectInContext.height)
effectInBuffer.rowBytes = effectInContext.bytesPerRow
UIGraphicsBeginImageContextWithOptions(self.size, false, screenScale)
let effectOutContext : CGContext = UIGraphicsGetCurrentContext()!
var effectOutBuffer = vImage_Buffer()
effectOutBuffer.data = effectOutContext.data
effectOutBuffer.width = vImagePixelCount(effectOutContext.width)
effectOutBuffer.height = vImagePixelCount(effectOutContext.height)
effectOutBuffer.rowBytes = effectOutContext.bytesPerRow
if ( hasBlur ) {
let inputRadius : Double = Double(blurRadius) * Double(screenScale)
var radius = floor(inputRadius * 3.0 * sqrt(2 * .pi) / 4 + 0.5)
let result = radius.truncatingRemainder(dividingBy: 2)
if (result != 1) {
radius += 1 // force radius to be odd so that the three box-blur methodology works.
}
var unsafePointer : UInt8 = 0
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, nil, 0, 0, UInt32(radius), UInt32(radius), &unsafePointer, UInt32(kvImageEdgeExtend))
vImageBoxConvolve_ARGB8888(&effectOutBuffer, &effectInBuffer, nil, 0, 0, UInt32(radius), UInt32(radius), &unsafePointer, UInt32(kvImageEdgeExtend))
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, nil, 0, 0, UInt32(radius), UInt32(radius), &unsafePointer, UInt32(kvImageEdgeExtend))
}
var effectImageBuffersAreSwapped : Bool = false
if ( hasSaturationChange ) {
let s : Double = saturationDeltaFactor
let floatingPointSaturationMatrix : [Double] = [
0.0722 + 0.9278 * s, 0.0722 - 0.0722 * s, 0.0722 - 0.0722 * s, 0,
0.7152 - 0.7152 * s, 0.7152 + 0.2848 * s, 0.7152 - 0.7152 * s, 0,
0.2126 - 0.2126 * s, 0.2126 - 0.2126 * s, 0.2126 + 0.7873 * s, 0,
0, 0, 0, 1,
]
let divisor : Int32 = 256
let matrixSize : Int = MemoryLayout.size(ofValue: floatingPointSaturationMatrix) / MemoryLayout.size(ofValue: floatingPointSaturationMatrix[0])
var saturationMatrix = [Int16]()
for _ in 0...matrixSize {
saturationMatrix.append(0)
}
for i in 0...matrixSize {
saturationMatrix[i] = Int16(roundf(Float(floatingPointSaturationMatrix[i]) * Float(divisor)))
}
if (hasBlur) {
vImageMatrixMultiply_ARGB8888(&effectOutBuffer, &effectInBuffer, saturationMatrix, divisor, nil, nil, UInt32(kvImageNoFlags))
effectImageBuffersAreSwapped = true
}
else {
vImageMatrixMultiply_ARGB8888(&effectInBuffer, &effectOutBuffer, saturationMatrix, divisor, nil, nil, UInt32(kvImageNoFlags))
}
}
if ( !effectImageBuffersAreSwapped ) {
effectImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}
if ( effectImageBuffersAreSwapped ) {
effectImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}
}
// 开启上下文 用于输出图像
UIGraphicsBeginImageContextWithOptions(self.size, false, screenScale)
let outputContext : CGContext = UIGraphicsGetCurrentContext()!
outputContext.scaleBy(x: 1.0, y: -1.0)
outputContext.translateBy(x: 0, y: -self.size.height)
// 开始画底图
outputContext.draw(self.cgImage!, in: imageRect)
//CGContextDrawImage(outputContext, imageRect, self.cgImage)
// 开始画模糊效果
if (hasBlur) {
outputContext.saveGState()
if ( (maskImage) != nil) {
outputContext.draw(maskImage!.cgImage!, in: imageRect)
}
else
{
outputContext.draw(effectImage.cgImage!, in: imageRect)
}
//CGContextDrawImage(outputContext, imageRect, effectImage.cgImage)
outputContext.restoreGState()
}
// 添加颜色渲染
if ( tintColor != nil ) {
outputContext.saveGState()
outputContext.setFillColor(tintColor!.cgColor)
outputContext.fill(imageRect)
outputContext.restoreGState()
}
// 输出成品,并关闭上下文
let outputImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return outputImage
}