在绘图应用程序中合并图像视图 swift

merging imageviews in drawing app swift

我正在开发绘图应用程序。我有三个图像视图 -

imageView - 包含基本图像

tempImageView - 用于绘图注释。 drawLineFrom 函数取一个点然后在 tempImageView

上画线
  func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint)
    {
        //print("drawLineFrom")
        let mid1 = CGPoint(x:(prevPoint1.x + prevPoint2.x)*0.5, y:(prevPoint1.y + prevPoint2.y)*0.5)
        let mid2 = CGPoint(x:(toPoint.x + prevPoint1.x)*0.5, y:(toPoint.y + prevPoint1.y)*0.5)
        UIGraphicsBeginImageContextWithOptions(self.tempImageView.boundsSize, false, 0.0)
        if let context = UIGraphicsGetCurrentContext()
        {
            tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.tempImageView.frame.size.width, height: self.tempImageView.frame.size.height))
            let annotaionPath = UIBezierPath()
            annotaionPath.move(to:  CGPoint(x: mid1.x, y: mid1.y))
            annotaionPath.addQuadCurve(to: CGPoint(x:mid2.x,y:mid2.y), controlPoint: CGPoint(x:prevPoint1.x,y:prevPoint1.y))
            annotaionPath.lineCapStyle = CGLineCap.round
            annotaionPath.lineJoinStyle = CGLineJoin.round
            annotaionPath.lineWidth = editorPanelView.brushWidth
            context.setStrokeColor(editorPanelView.drawingColor.cgColor)
            annotaionPath.stroke()
            tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
            tempImageView.alpha = editorPanelView.opacity
            UIGraphicsEndImageContext()
        }
    }

drawingImageView - 在每个 touchesEnded 方法之后,我将 tempImageView 与 drawingImageView 合并并设置 tempImageView.image = nil 。

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        isDrawing = false
        if !swiped
        {
            drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
        }
        annotationArray.append(annotationsPoints)
        annotationsPoints.removeAll()
        // Merge tempImageView into drawingImageView
        UIGraphicsBeginImageContext(drawingImageView.frame.size)
        drawingImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingImageView.frame.size.width, height: drawingImageView.frame.size.height), blendMode: CGBlendMode.normal, alpha: 1.0)
        tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingImageView.frame.size.width, height: drawingImageView.frame.size.height), blendMode: CGBlendMode.normal, alpha: editorPanelView.opacity)
        drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        tempImageView.image = nil
    }

点击保存按钮时,

let drawingImage = self.drawingImageView.image
let combinedImage = self.imageView.combineWithOverlay(overlayImageView: self.drawingImageView)

我正在保存 combinedImage。

问题是,当我将 tempImage 视图与绘图图像视图合并时,注释变得 blurred.I 想要保持相同的清晰度。我找不到任何解决方案。任何帮助(即使只是朝着正确的方向踢)将不胜感激。

我认为问题在于使用 UIGraphicsBeginImageContext(drawingImageView.frame.size)

它使用的默认比例是 1.0 因此,如果您使用的是视网膜屏幕,它会导致内容被放大 2 或 3 倍,从而导致外观模糊。

您应该像在 drawLineFrom 中那样使用 UIGraphicsBeginImageContextWithOptions,比例为 0.0,默认为屏幕比例。