图片的裁剪/优化

 /// 给指定的图片进行拉伸

    ///

    func avatarImage(image: UIImage, size:CGSize) -> UIImage? {

        

        let rect = CGRect(origin: CGPoint(), size: size)

        

        

        //上下文

        /*

         * size: 绘图尺寸

         *不透明  false (透明)

         *屏幕分辨率,默认使用 1.0,图像质量不好; 0 表示当前屏幕分辨率

         */

        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)

        

        //绘图

        image.draw(in: rect)


        //取结果

        let result = UIGraphicsGetImageFromCurrentImageContext()

        

        //关闭上下文

        UIGraphicsEndImageContext()

        

        return result

        

        

    }

    

    //MARK:设置圆角半径

    func setcornerRadiusImage(image: UIImage, size:CGSize) -> UIImage? {

        

        let rect = CGRect(origin: CGPoint(), size: size)

        

        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)

        

        //背景 填充

        UIColor.red.setFill()

        //填充

        UIRectFill(rect)

        

        //圆路径

        let path = UIBezierPath(ovalIn: rect)

        

        //裁切

        path.addClip()

        

        image.draw(in: rect)

        

        //绘制内切的圆形

        UIColor.darkGray.setStroke()

        //绘制边线

        path.lineWidth = 2.0

        path.stroke()

        

        //取结果

        let result = UIGraphicsGetImageFromCurrentImageContext()

        

        //关闭上下文

        UIGraphicsEndImageContext()

        

        return result

        

    }


猜你喜欢

转载自blog.csdn.net/qq_24143647/article/details/72845757