快速裁剪图​​像 [英] Crop image in swift

查看:88
本文介绍了快速裁剪图​​像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to crop image in swift. I'm trying to implement something like, user will capture a photo. Once photo is captured user will be allowed to set the crop area. I'm able to get the image from that crop area, but I want that the crop image should be resized to particular width and height. That is, if particular height or width is smaller then it should be resized.

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

This image should be of frame of it's maximum width and height. Currently it is just adding transparency to the other area.

我还添加了用于裁剪的代码

I had also added my code for cropping

            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)

任何帮助都是有意义的

预先感谢

推荐答案

使用此库以特定于用户的方式裁剪图像

Use this Library to crop image as User Specific

https://github.com/kishikawakatsumi/PEPhotoCropEditor

谢谢 希望这会对您有所帮助!

Thanks Hope this will help you!

这篇关于快速裁剪图​​像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆