I have an image:
That I would like to apply to a UIView
to make the waveform (the transparent area) coloured. I have tried the following:
let image = UIImage(named: "waveform")!
let view = UIView(frame: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
let mask = CALayer()
mask.frame = view.bounds
mask.contents = image.cgImage!
view.layer.mask = mask
view.layer.masksToBounds = true
view.backgroundColor = UIColor.green
But this gives me the opposite effect, where the waveform is cut out:
I'd like the outside area of the waveform to be transparent, and the inside to be coloured. I plan to place the UIView
on top of another view, so I think it needs to be a mask.
I tried using CIImage
to invert the transparent and coloured area as suggested in similar questions:
let cgImage = image.cgImage!
let ciImage = CIImage(cgImage: cgImage)
let filter = CIFilter(name: "CIColorInvert")!
filter.setValue(ciImage, forKey: kCIInputImageKey)
mask.contents = filter.outputImage!.cgImage
...but this resulted in a blank view
(transparent background).
Any help would be much appreciated.