斯威夫特:在图像上绘制而不改变其大小(方面填充)

问题描述:

我有一个简单的视图控制器,其中整个屏幕是一个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() 
    } 

这条线:

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

在屏幕的宽高比绘制的图像,但图像可能是不相同的纵横比(宽度比到高度)。

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