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

时间:2018-07-26 14:43:20

标签: swift uiimageview core-graphics uibezierpath

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

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视图与绘图图像视图合并时,注释变得模糊了。我想保持相同的清晰度。我无法为此找到任何解决方案。任何帮助(即使只是朝着正确的方向踢)也将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为问题在于使用project.json

它使用的默认缩放比例是UIGraphicsBeginImageContext(drawingImageView.frame.size),因此,如果您使用的是视网膜屏幕,它将导致内容放大2或3倍,从而导致外观模糊。

您应该像在1.0中一样使用UIGraphicsBeginImageContextWithOptions,使用drawLineFrom的比例尺,该比例尺将默认为屏幕比例尺。