Reputation: 4519
I have an image that I want to crop at an specific shape. So I used this:
var aPath = UIBezierPath()
// Set the starting point of the shape.
aPath.moveToPoint(CGPointMake(0.0, 0.0))
// Draw the lines.
aPath.addLineToPoint(CGPointMake(50.0, 0.0))
aPath.addLineToPoint(CGPointMake(50.0, 50.0))
aPath.addLineToPoint(CGPointMake(0.0, 50.0))
aPath.closePath()
var imgView = UIImageView(image: UIImage(named: "kat.jpg")!)
imgView.frame = CGRectMake(0, 0, 100, 100)
self.view.addSubview(imgView)
But it doesn't work. It picks the original image, and resizes it to the height and with of 100px. But I want to crop it inside the points.
Upvotes: 0
Views: 587
Reputation: 5588
You should create an CAShapeLayer from your path, and the assign this layer to the mask
property of your image view
Upvotes: 2