迅速裁剪图像

时间:2015-06-04 11:54:00

标签: swift uiimageview uigraphicscontext image-masking

我正试图快速裁剪图​​像。我试图实现类似的功能,用户将拍摄照片。拍摄照片后,将允许用户设置裁剪区域。我能够从该裁剪区域获取图像,但我希望将裁剪图像调整为特定的宽度和高度。也就是说,如果特定的高度或宽度较小,则应调整其大小。

enter image description here

此图像应为其最大宽度和高度的框架。目前它只是为其他区域增加透明度。

我还添加了我的裁剪代码



            let tempLayer = CAShapeLayer()
            tempLayer.frame = self.view.frame
            
            let path = UIBezierPath()
            var endPoint: CGPoint!
            
            for (var i = 0; i<4; i++){
                let tag = 101+i
                let pointView = viewCrop.viewWithTag(tag)
                switch (pointView!.tag){
                case 101:
                    endPoint = CGPointMake(pointView!.center.x-20, pointView!.center.y-20)
                    path.moveToPoint(endPoint)
                default:
                    path.addLineToPoint(CGPointMake(pointView!.center.x-20, pointView!.center.y-20))
                }
            }
            
            path.addLineToPoint(endPoint)
            path.closePath()
            tempLayer.path = path.CGPath
            
            tempLayer.fillColor = UIColor.whiteColor().CGColor
            tempLayer.backgroundColor = UIColor.clearColor().CGColor
            imgReceiptView.layer.mask = tempLayer
            
            UIGraphicsBeginImageContextWithOptions(viewCrop.bounds.size, imgReceiptView.opaque, 0.0);
            imgReceiptView.layer.renderInContext(UIGraphicsGetCurrentContext())
            let cropImg = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext();
            
            UIImageWriteToSavedPhotosAlbum(cropImg, nil, nil, nil)
            imgReceiptView.hidden = true
            
            let tempImageView = UIImageView(frame: CGRectMake(20,self.view.center.y-80, self.view.frame.width-40,160))
            tempImageView.backgroundColor = UIColor.grayColor()
            tempImageView.image = cropImg
            tempImageView.tag = 1001
            tempImageView.layer.masksToBounds = true
            self.view.addSubview(tempImageView)
&#13;
&#13;
&#13;

任何帮助都会很明显

提前致谢

1 个答案:

答案 0 :(得分:0)

使用此库将图像裁剪为用户特定

https://github.com/kishikawakatsumi/PEPhotoCropEditor

由于 希望这会对你有所帮助!