我正在开发绘图应用程序。我有三个图像视图-
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-在每种触控方法之后,我将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)

我在拯救组合图像。
问题是,当我将tempImageView与DrawingImageView合并时,批注会变得模糊。我希望保持同样的清晰度。我找不到任何解决办法。任何帮助(即使只是朝着正确的方向踢一脚)都将不胜感激。

最佳答案

我认为问题在于使用UIGraphicsBeginImageContext(drawingImageView.frame.size)
它使用的默认比例是1.0,因此如果您使用的是视网膜屏幕,它将导致内容放大2或3倍,导致外观模糊。
您应该像在UIGraphicsBeginImageContextWithOptions中一样使用drawLineFrom,使用默认为屏幕比例的0.0

关于swift - 在绘图应用程序中合并 ImageView ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51541618/

10-13 08:23