图片的裁剪/优化

来源:互联网 发布:数据库中存储的是什么 编辑:程序博客网 时间:2024/05/21 06:24

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

    ///

    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

        

    }


原创粉丝点击