简体   繁体   中英

Crop image on uibezierpath swift

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.

您应该从路径创建CAShapeLayer,并将此层分配给图像视图的mask属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM