我使用vImageBoxConvolve_ARGB8888函数来模糊UIImage。代码如下所示:
class MyLayer: CALayer, CAAnimationDelegate{
func updateBounds(_ bounds: CGRect){
nextBounds = bounds
self.bounds = bounds
}
private var nextBounds : CGRect!
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if let keyPath = (anim as! CABasicAnimation).keyPath , let displayLink = animationLoops[keyPath]{
displayLink?.invalidate()
animationLoops.removeValue(forKey: keyPath) }
}
@objc func updateDrawing(displayLink: CADisplayLink){
bounds = presentation()!.bounds
setNeedsDisplay()
}
var animationLoops: [String : CADisplayLink?] = [:]
override func action(forKey event: String) -> CAAction? {
let result = super.action(forKey: event)
if(event == "bounds"){
guard let result = result as? CABasicAnimation,
animationLoops[event] == nil
else{return nil}
let anim = CABasicAnimation.init(keyPath: result.keyPath)
anim.fromValue = result.fromValue
anim.toValue = nextBounds
let caDisplayLink = CADisplayLink.init(target: self, selector: #selector(updateDrawing))
caDisplayLink.add(to: .main, forMode: RunLoop.Mode.default)
anim.delegate = self
animationLoops[event] = caDisplayLink
return anim
}
return result
}
vImageBoxConvolve_ARGB8888函数接受背景颜色参数(第8个参数),该参数的类型为UnsafePointer。这里的参数为nil,但我想将其设置为红色。我不知道该怎么做。如果有人可以提供任何提示,我将不胜感激。预先感谢。
答案 0 :(得分:0)
您可以这样做:
let redPointer = UnsafePointer<UInt8>([0xFF, 0x00, 0x00])
var error = vImageBoxConvolve_ARGB8888(&inBuffer,
&outBuffer,
nil,
0,
0,
UInt32(boxSize),
UInt32(boxSize),
redPointer,
vImage_Flags(kvImageBackgroundColorFill))
请记住,标记kvImageBackgroundColorFill
和kvImageEdgeExtend
是mutually exclusive,因此您不能在flags参数中传递kvImageBackgroundColorFill + kvImageEdgeExtend
。