iOS裁切一个圆形图片-swift

// 图片裁切
        let orgImag = UIImage(named: "2.jpg")
        // 1 开启位图上下文  跟图片的尺寸一样大
        UIGraphicsBeginImageContextWithOptions((orgImag?.size)!, false, 0)

        // 2 设置原型裁剪区域, 正切与图片
        let path = UIBezierPath(ovalInRect: CGRectMake(0, 0, (orgImag?.size.height)!, (orgImag?.size.height)!))

        // 2.1 把路径设置为裁切区域
        path.addClip()

        // 3 绘制图片
        orgImag?.drawAtPoint(CGPointZero)

        // 4 从上下文中获取图片
        let ovrWater = UIGraphicsGetImageFromCurrentImageContext()

        // 5 关闭上下文
        UIGraphicsEndImageContext()

        let imageView = UIImageView(frame: CGRectMake(0, 0, 250, 250))
        imageView.image = ovrWater
        imageView.backgroundColor = UIColor.clearColor()
        imageView.contentMode = UIViewContentMode.ScaleAspectFit

        view.addSubview(imageView)

你可能感兴趣的:(iOS裁切一个圆形图片-swift)