CGContext & CGMutablePath рисует линию, но она должна быть обратной - PullRequest
0 голосов
/ 01 ноября 2019

Я использую CgContext & CGMutablePath для рисования объектов, но нарисованное изображение будет перевернуто. Мне нужен проект рисования объекта в CoreMl.

Я использую события Touch для рисования объекта Но когда я рисую в верхнем направлении, если будет в нижнем направлении. Я хочу, чтобы линия была в правильном направлении

Я учусь у ссылка

func drawContextLine(from fromPoint: CGPoint, to toPoint:CGPoint) ->CGImage?{

        let grayscale = CGColorSpaceCreateDeviceGray()
        if contextDraw == nil{
            contextDraw = CGContext(
            data:nil, width:256, height:256, bitsPerComponent:8, bytesPerRow:0,
            space:grayscale, bitmapInfo:CGImageAlphaInfo.none.rawValue)
        }

        //        intermediate_bitmap_context?.setStrokeColor(
        //            red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        contextDraw?.setStrokeColor(UIColor.white.cgColor)

        let path = CGMutablePath()
        path.move(to: fromPoint)
        path.addLine(to: toPoint)

        contextDraw?.setLineWidth(5.0)
        contextDraw?.beginPath()
        contextDraw?.addPath(path)
        contextDraw?.strokePath()

        guard let imgCg = contextDraw?.makeImage() else {
            return nil
        }
        imgViewPridict.image = UIImage.init(cgImage: imgCg)
        return contextDraw?.makeImage()

    }

начало касания

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          if let touch = touches.first{
              lastPoint = touch.location(in: self.view)

          }
    }

движение касания

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard touches.first != nil else {
            return
        }
        if let touch = touches.first {

            let currentPoint = touch.location(in: self.view)
            drawLine(from: lastPoint, to: currentPoint)
            drawContextLine(from: lastPoint, to: currentPoint)
            lastPoint = currentPoint
        }
    }

1 Ответ

0 голосов
/ 01 ноября 2019

Попробуйте приведенный ниже фрагмент кода.

func drawContextLine(from fromPoint: CGPoint, to toPoint:CGPoint) ->CGImage?{

        let grayscale = CGColorSpaceCreateDeviceGray()
        if contextDraw == nil{
            contextDraw = CGContext(
            data:nil, width:256, height:256, bitsPerComponent:8, bytesPerRow:0,
            space:grayscale, bitmapInfo:CGImageAlphaInfo.none.rawValue)
        }

        contextDraw?.setStrokeColor(UIColor.white.cgColor)

        contextDraw?.saveGState()
        contextDraw?.translateBy(x: 0, y: CGFloat(integerLiteral: contextDraw?.height ?? 0))
        contextDraw?.scaleBy(x: 1.0, y: -1.0)

        contextDraw?.setLineWidth(5.0)
        contextDraw?.move(to: CGPoint(x: 100, y: 100))
        contextDraw?.addLine(to: CGPoint(x: 150, y: 150))
        contextDraw?.strokePath()

        contextDraw?.restoreGState()

        guard let imgCg = contextDraw?.makeImage() else {
            return nil
        }
        imgViewPridict.image = UIImage.init(cgImage: imgCg)
        return contextDraw?.makeImage()

    }

Вам может понадобиться инвертировать содержимое, нарисованное в cgContext, применив к нему AffineTransform.

...