I'm working on an app where I draw a UIView via code and I fill it with a UIColor. Next thing I want to load a mask.png file (no transparency, just black and white) and mask the UIView to change its visual appearance.
Any idea how to do this?
I'm working on an app where I draw a UIView via code and I fill it with a UIColor. Next thing I want to load a mask.png file (no transparency, just black and white) and mask the UIView to change its visual appearance.
Any idea how to do this?
You might be able to use CGContextClipToMask:
I have create a category with the code above :
UIView+ImageMask.h :
UIView+ImageMask.m :
iOS 8 introduces the
maskView
property which allows you to use an alternate view to provide the mask image. In Swift:Note that you need to supply an image which actually has transparency; an image with white parts (for opaque) and black parts (for transparent) won't work.
Swift 2.0
Pass a UIImage and a mask image to this function, and you will get a masked image as a UIImage.
Remember to import QuartzCore and add the framework
It is very easy but I didn't find the answer anywhere!
Thanks drawnonward,
ended up with this function: