I'm writing in Swift, trying to mask an image using UIImage and CALayer. The problem is that the mask is scaled and shifted when applied to the image. I have created my UIImageViewer using the Interface Builder and I have set it to Aspect Fit. I would like to mask images of arbitrary size. I generate the mask image to be the same size as the image. This is a requirement because later I want to add more elements to my mask programmatically and it has to cover the masked image. Here's my code:
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var img = UIImage(named: "flowers.png")
var maskImageSize = CGSizeMake(img!.size.width, img!.size.height)
UIGraphicsBeginImageContextWithOptions(maskImageSize, false, 0.0)
var color = UIColor(white: 1.0, alpha: 0.0)
color.setFill()
var rect = CGRectMake(0, 0, img!.size.width, img!.size.height)
UIRectFill(rect)
color = UIColor(white: 0.0, alpha: 1.0)
color.setFill()
rect = CGRectMake((img!.size.width/2)-50, (img!.size.height/2)-50, 100, 100)
UIRectFill(rect)
var maskImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var maskLayer = CALayer()
maskLayer.frame = self.imageView.layer.bounds // it'll have the same problem if I set it to self.imageView.layer.frame
maskLayer.contents = maskImage.CGImage
maskLayer.contentsGravity = kCAGravityCenter
self.imageView.image = img
self.imageView.layer.mask = maskLayer;
self.imageView.layer.masksToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I need to know how to align the image mask onto the original image. Thanks for your help!
I have pretty good screen shots but Stackoverflow doesn't let me to post them because I don't have enough reputation!