2017-01-05 14 views
1

私はこのコードを使用して、ユーザーが画面上をタッチしたときにイメージの一部をクリアします。私はお互いを上書きする複数の画像を持っています。今すぐ上のほとんどのレイヤーは正常に動作しているので、下の画像の一部が表示されます。 今私が達成したいのは、ユーザーが選択したクローズパスであり、選択されたクローズパスの領域がクリアされるはずです。 複数のレイヤーを選択して、カットする部分を選択できます。 たとえば、8つの画像があり、ユーザーがレイヤー6〜8を選択した場合、可視部分はユーザーがタッチで消去するレイヤー5になります。グラフィックスコンテキストを使用して、close pathを使用して任意のimageViewのイメージをクリアする方法

func drawBrushOnLayer(fromPoint: CGPoint, toPoint: CGPoint , selected:[Int]) {    

      UIGraphicsBeginImageContext(DrawImage.frame.size) 

    var context = UIGraphicsGetCurrentContext() 
      DrawImage.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height - 50)) 

      context?.move(to: fromPoint) 

      context?.addLine(to: toPoint) 

      context?.setLineCap(.butt) 

      context?.setLineWidth(BrushSize) 

      context?.setBlendMode(.clear) 

      context?.setShouldAntialias(false) 

      UIColor.clear.set() 

      context?.strokePath() 

      DrawImage.image = UIGraphicsGetImageFromCurrentImageContext()! 

      UIGraphicsEndImageContext() 

} 

今のところ私は画像に白い色を描いていますが、選択した部分をクリアする必要がありますので、下の画像を見ることができます。

func drawFill(point : CGPoint) { 

     autoreleasepool{ 

      UIGraphicsBeginImageContext(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 50)) 

      DrawImage .draw(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 50)) 

      UIColor.white.set() 

      BezierPath.addLine(to: point) 

      BezierPath.lineWidth = 2.0 

      BezierPath .close() 

      BezierPath.fill() 

      let context = UIGraphicsGetCurrentContext() 

      context?.addPath(lassoBezier.cgPath) 

      newImage = UIGraphicsGetImageFromCurrentImageContext()! 

      DrawImage.image = newImage 

       UIGraphicsEndImageContext() 

     } 

} 
+0

おかげNirav編集すると問題がより明確になります。 – ChanWarde

答えて

0

FUNCのdrawFill(点:するCGPoint){

autoreleasepool{ 

     UIGraphicsBeginImageContext(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 50)) 

     DrawImage .draw(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 50)) 

     BezierPath.addLine(to: point) 

     BezierPath .close() 

     let context = UIGraphicsGetCurrentContext() 
     UIColor.clear.set() 
     context?.addPath(lassoBezier.cgPath) 
     context?.setLineCap(.square) 
     context?.setBlendMode(.clear) 
     context?.setShouldAntialias(false) 
     context?.fillPath() 
     newImage = UIGraphicsGetImageFromCurrentImageContext()! 

     DrawImage.image = newImage 

      UIGraphicsEndImageContext() 

    } 

}

関連する問題