好的,我在Swift工作,有很多像How to use UIVisualEffectView?这样的答案,讨论如何在图像上应用UIVisualEffectView,以便它像背景一样模糊它。
我的问题是我需要让我的图像,或者更确切地说我的图像轮廓是模糊视图 - 这意味着我在图像的形状中创建了一个模糊的UIVisualEffectView,因此图像本身的“颜色”就是模糊。示例模型(假装模糊):
我知道你可以将UIImage跟踪成这样的自定义颜色:
func overlayImage(color: UIColor, img: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(img.size, false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
color.setFill()
context!.translateBy(x: 0, y: img.size.height)
context!.scaleBy(x: 1.0, y: -1.0)
context!.setBlendMode(CGBlendMode.colorBurn)
let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
context!.draw(img.cgImage!, in: rect)
context!.setBlendMode(CGBlendMode.sourceIn)
context!.addRect(rect)
context!.drawPath(using: CGPathDrawingMode.fill)
let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return coloredImage!
}
但我不能让我的UIImageView“屏蔽”模糊视图并实现效果。现在有了这个尝试:
var img = UIImageView(image: UIImage(named: "dudeIco"))
img.frame = CGRect(x: 0, y: 0, width: self.bounds.width * 0.7, height: self.bounds.width * 0.7)
img.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
self.addSubview(img)
let blur = UIVisualEffectView(effect: UIBlurEffect(style:
UIBlurEffectStyle.light))
blur.frame = img.bounds
blur.isUserInteractionEnabled = false
img.insertSubview(blur, at: 0)
我只是得到一个模糊的方块。我需要图像的形状。我怎样才能做到这一点?这不可能吗?