2017-04-05 6 views
0

私はUIImageViewサブクラスを実装しようとしています。これにより、ユーザーは指で画像ビューを描画できます。私のUIImageViewのcontentModeはAspect Fillに設定されています。私は描画コードが実行され、結果のイメージがグラフィックスコンテキストから抽出されたときに、それがScale to Fill型のフォーマットにスケールされていることに気付きました。これは私が望むものではありません。私はこの画像の抽出と画像のアスペクト比の維持をどのように達成するかを知っている人がいるのだろうかと思っていました。このコードではUIGraphicsGetImageFromCurrentContextとアスペクト比を維持する

class DrawImageView : UIImageView { 

    var lastTouchPoint = CGPoint.zero 
    var isSwiping = false 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     isSwiping = false 
     if let touchPoint = touches.first { 
      lastTouchPoint = touchPoint.location(in: self) 
     } 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     isSwiping = true 

     if let touchPoint = touches.first { 
      let currentPoint = touchPoint.location(in: self) 
      UIGraphicsBeginImageContext(frame.size) 

      image?.draw(in: CGRect(x:0, y:0, width:frame.size.width, height:frame.size.height)) 

      if let context = UIGraphicsGetCurrentContext() { 
       context.move(to: lastTouchPoint) 
       context.addLine(to: currentPoint) 
       context.setLineCap(CGLineCap.round) 
       context.setLineWidth(9.0) 
       context.setStrokeColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0) 
       context.strokePath() 
       image = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       lastTouchPoint = currentPoint 
      } 
     } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if(!isSwiping) { 

      UIGraphicsBeginImageContext(frame.size) 
      image?.draw(in: CGRect(x:0, y:0, width:frame.size.width, height:frame.size.height)) 


      if let context = UIGraphicsGetCurrentContext() { 
       context.setLineCap(CGLineCap.round) 
       context.setLineWidth(9.0) 
       context.setStrokeColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0) 
       context.move(to: lastTouchPoint) 
       context.addLine(to: lastTouchPoint) 
       context.strokePath() 
       image = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
      } 
     } 
    } 
} 

答えて

1

:「 - に関係なく、サイズやアスペクト比の - この完全な画像を描画幅×高さ寸法の長方形に」あなたが言っている

UIGraphicsBeginImageContext(frame.size) 
image?.draw(in: CGRect(x:0, y:0, width:frame.size.width, height:frame.size.height)) 

たとえば、imageのサイズが「ネイティブ」300 x 200、frameのサイズが200 x 200の場合は、画像が描画されます。

これを避けるには、描画する適切な矩形を計算する必要があります。これらのサイズを使用して、あなたは(アスペクト塗りつぶし)やりたいと思います。もちろん

image?.draw(in: CGRect(x:-50, y:0, width:frame.size.width + 50, height:frame.size.height)) 

を、代わりにハードコードされた値を、あなたは実際の数値を決定するために迅速な計算を実行すると思います。次のように

0

私が発見してしまった機能とを提供するために使用して、両方の側面は、RECT、アスペクトフィットRECTを埋めるには、次のとおりです。

func getAspectFillFrame(sizeImageView : CGSize, sizeImage: CGSize) -> CGRect { 

     let aspect = sizeImage.width/sizeImage.height 
     let rect: CGRect 

     if sizeImageView.width/aspect > sizeImageView.height { 

      let height = sizeImageView.width/aspect 

      rect = CGRect(x: 0, y: (sizeImageView.height - height)/2, 
          width: sizeImageView.width, height: height) 

     } else { 
      let width = sizeImageView.height * aspect 
      rect = CGRect(x: (sizeImageView.width - width)/2, y: 0, 
          width: width, height: sizeImageView.height) 
     } 

     return rect 

    } 

    func getAspectFitFrame(sizeImgView:CGSize, sizeImage:CGSize) -> CGRect { 

     let imageSize:CGSize = sizeImage 
     let viewSize:CGSize = sizeImgView 

     let hfactor : CGFloat = imageSize.width/viewSize.width 
     let vfactor : CGFloat = imageSize.height/viewSize.height 

     let factor : CGFloat = max(hfactor, vfactor) 

     // Divide the size by the greater of the vertical or horizontal shrinkage factor 
     let newWidth : CGFloat = imageSize.width/factor 
     let newHeight : CGFloat = imageSize.height/factor 

     var x:CGFloat = 0.0 
     var y:CGFloat = 0.0 
     if newWidth > newHeight{ 
      y = (sizeImgView.height - newHeight)/2 
     } 
     if newHeight > newWidth{ 
      x = (sizeImgView.width - newWidth)/2 
     } 
     let newRect:CGRect = CGRect(x: x, y: y, width: newWidth, height: newHeight) 

     return newRect 

    } 
関連する問題