Swift:在图像上绘制而不更改其大小(纵横比填充) [英] Swift: Draw on image without changing its size (Aspect fill)

查看:171
本文介绍了Swift:在图像上绘制而不更改其大小(纵横比填充)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple view controller where the entire screen is an imageView. The imageView has its content mode set to UIViewContentMode.scaleAspectFill. I can draw on the picture, but as soon as I do the image shrinks horizontally. This messing up the quality of the image and I'm not sure exactly why it's happening.

我看着这个问题,但我听不懂唯一的答案。我认为问题出在我的绘画方式上,而rect正在缩小图像。

I looked at this question, but I didn't understand the only answer. I think the issue is with the way I am drawing and that rect is shrinking the image.

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
        }
    }
}

更新-解决方案

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

I finally got back to working on this problem. Looking at this question, I was able to use the accepted answer to solve my problem. Below is my updated code:

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()
    }


推荐答案

此行:

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

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

Draws the image at the aspect ratio of the screen, but your image is probably not at the same aspect ratio (the ratio of width to height).

我认为这可能会有用: UIImage Aspect Fit当使用drawInRect时?

I think this might be useful: UIImage Aspect Fit when using drawInRect?

这篇关于Swift:在图像上绘制而不更改其大小(纵横比填充)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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