我正在开发绘图应用程序。我有三个 ImageView -
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 View 与绘图 ImageView 合并时,注释变得模糊。我想保持相同的清晰度。我找不到任何解决方案。我们将不胜感激任何帮助(即使只是朝着正确的方向踢)。
最佳答案
我认为问题在于使用 UIGraphicsBeginImageContext(drawingImageView.frame.size)
。
它使用的默认比例是 1.0
,所以如果您使用的是视网膜屏幕,它会导致内容被放大 2 或 3 倍,从而导致外观模糊。
您应该像在 drawLineFrom
中那样使用 UIGraphicsBeginImageContextWithOptions
,比例为 0.0
,默认为屏幕比例。
关于swift - 在绘图应用程序swift中合并 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541618/