Swift:在不改变尺寸的情况下绘制图像(Aspect填充)

时间:2017-09-25 19:42:00

标签: ios swift uiimageview draw

我有一个简单的视图控制器,整个屏幕是一个imageView。 imageView的内容模式设置为UIViewContentMode.scaleAspectFill。我可以在图片上画画,但是一旦我这样做,图像就会水平缩小。这弄乱了图像的质量,我不确定它为什么会发生。

我看了this question,但我不明白唯一的答案。我认为问题在于我绘画的方式,并且矩形正在缩小图像。

class AnnotateViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    var lastPoint = CGPoint.zero
    var fromPoint = CGPoint()
    var toPoint = CGPoint.zero
    var brushWidth: CGFloat = 5.0
    var opacity: CGFloat = 1.0


     @IBAction func startDrawing(_ sender: Any) {
        self.isDrawing = !self.isDrawing
        if isDrawing {
            self.imageView.isUserInteractionEnabled = true
            showColorButtons()
        }
        else {
            self.imageView.isUserInteractionEnabled = false
            hideColorButtons()
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            lastPoint = touch.preciseLocation(in: self.imageView)
        }
    }

    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
        UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, false, 0.0)
        imageView.image?.draw(in: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))

        let context = UIGraphicsGetCurrentContext()
        context?.move(to: fromPoint)
        context?.addLine(to: toPoint)


        context?.setLineCap(CGLineCap.round)
        context?.setLineWidth(brushWidth)
        context?.setStrokeColor(red: 255, green: 0, blue: 0, alpha: 1.0)
        context?.setBlendMode(CGBlendMode.normal)
        context?.strokePath()

        imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        imageView.alpha = opacity
        UIGraphicsEndImageContext()

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.preciseLocation(in: self.imageView)
            drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
            lastPoint = currentPoint
        }
    }
}

更新 - 解决方案

我终于回到了解决这个问题的方法。看this question,我能够使用接受的答案来解决我的问题。以下是我更新的代码:

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, true, 0.0)

        let aspect = imageView.image!.size.width / imageView.image!.size.height
        let rect: CGRect
        if imageView.frame.width / aspect > imageView.frame.height {
            let height = imageView.frame.width / aspect
            rect = CGRect(x: 0, y: (imageView.frame.height - height) / 2,
                          width: imageView.frame.width, height: height)
        } else {
            let width = imageView.frame.height * aspect
            rect = CGRect(x: (imageView.frame.width - width) / 2, y: 0,
                          width: width, height: imageView.frame.height)
        }
        imageView.image?.draw(in: rect)

        let context = UIGraphicsGetCurrentContext()
        context?.move(to: fromPoint)
        context?.addLine(to: toPoint)
        context?.setLineCap(CGLineCap.round)
        context?.setLineWidth(brushWidth)
        context?.setStrokeColor(red: self.red, green: self.green, blue: self.blue, alpha: 1.0)
        context?.setBlendMode(CGBlendMode.normal)
        context?.strokePath()

        imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        annotatedImage = imageView.image
        imageView.alpha = opacity
        UIGraphicsEndImageContext()
    }

1 个答案:

答案 0 :(得分:1)

这一行:

imageView.image?.draw(in: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))

以屏幕的宽高比绘制图像,但您的图像可能没有相同的宽高比(宽高比)。

我认为这可能有用:UIImage Aspect Fit when using drawInRect?