2017-09-25 5 views
1

私は、画面全体がimageViewである単純なビューコントローラを持っています。 imageViewのコンテンツモードはUIViewContentMode.scaleAspectFillに設定されています。私は絵を描くことができますが、すぐにイメージが水平に収縮します。これはイメージの品質を混乱させ、なぜそれが起こっているのか正確にはわかりません。スウィフト:サイズを変更せずに画像に描画する(アスペクト塗りつぶし)

私はthis questionを見ましたが、唯一の答えは分かりませんでした。問題は私が描いている方法であり、rectは画像を縮小していると思います。

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

UPDATE - SOLUTION

私は最終的にこの問題に取り組んに戻りました。 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

このライン:

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

は、画面のアスペクト比で画像を描画しますが、あなたのイメージは同じアスペクト比(幅の比率で、おそらくではありません高さまで)。

これは役に立つと思われます。UIImage Aspect Fit when using drawInRect?

関連する問題