Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在嵌入UIScrollView的UIImage上自由绘制_Ios_Swift_Uiscrollview_Uiimage_Core Graphics - Fatal编程技术网

Ios 在嵌入UIScrollView的UIImage上自由绘制

Ios 在嵌入UIScrollView的UIImage上自由绘制,ios,swift,uiscrollview,uiimage,core-graphics,Ios,Swift,Uiscrollview,Uiimage,Core Graphics,我正在从事一个项目,该项目涉及在UIScrollView中的UIImage上徒手绘制。在默认缩放下,图形的一切都很好,但是如果我调整UIScrollView的缩放并再次尝试绘制,它会开始通过缩小图像并使图像看起来模糊来扭曲图像。它还将图像推送到屏幕的最左侧和最右侧。我尝试过使用不同的缩放选项,但没有效果 这是正常比例的图像,其中有一些图形。 这是缩放和绘制几秒钟后的图像 以下是相关代码(方法在包含滚动视图的视图控制器中声明) override func touchesBegan(_ touch

我正在从事一个项目,该项目涉及在UIScrollView中的UIImage上徒手绘制。在默认缩放下,图形的一切都很好,但是如果我调整UIScrollView的缩放并再次尝试绘制,它会开始通过缩小图像并使图像看起来模糊来扭曲图像。它还将图像推送到屏幕的最左侧和最右侧。我尝试过使用不同的缩放选项,但没有效果

这是正常比例的图像,其中有一些图形。

这是缩放和绘制几秒钟后的图像

以下是相关代码(方法在包含滚动视图的视图控制器中声明)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)              
    let currentPoint = touches.first!.location(in: drawingImageView) 
    UIGraphicsBeginImageContextWithOptions(drawingImageView.frame.size, false, 0)
    drawingImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingImageView.frame.size.width, height: drawingImageView.frame.size.height))

    UIGraphicsGetCurrentContext()?.addRect(CGRect(x: currentPoint.x, y: currentPoint.y, width: 1, height: 1))
    UIGraphicsGetCurrentContext()?.setLineCap(.round)
    UIGraphicsGetCurrentContext()?.setLineWidth(10)
    UIGraphicsGetCurrentContext()?.setStrokeColor(colorSelectorButton.backgroundColor!.cgColor)
    UIGraphicsGetCurrentContext()?.setBlendMode(.normal)
    UIGraphicsGetCurrentContext()?.strokePath()
    drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
让currentPoint=touchs.first!。位置(在:drawingImageView中)
UIGraphicsBeginImageContextWithOptions(drawingImageView.frame.size,false,0)
drawingImageView.image?.draw(in:CGRect(x:0,y:0,宽度:drawingImageView.frame.size.width,高度:drawingImageView.frame.size.height))
UIGraphicsGetCurrentContext()?.addRect(CGRect(x:currentPoint.x,y:currentPoint.y,宽度:1,高度:1))
UIGraphicsGetCurrentContext()?.setLineCap(.round)
UIGraphicsGetCurrentContext()?.setLineWidth(10)
UIGraphicsGetCurrentContext()?.setStrokeColor(colorSelectorButton.backgroundColor!.cgColor)
UIGraphicsGetCurrentContext()?.setBlendMode(.normal)
UIGraphicsGetCurrentContext()?.strokePath()的
drawingImageView.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
super.touchesend(触摸,带有:事件)
}

我最终重构了整个代码。我在imageView的顶部创建了一个临时UIView,用UIView的drawRect方法处理所有绘图。绘图完成后,我从视图中渲染了一个uiimage,并将其与下图合并。但此重构仍然不能解决原始问题,并且此引用actored way使任何图像擦除功能的实现都更具挑战性。我最终重构了整个代码。我在imageView的顶部创建了一个临时UIView,它处理UIView的drawRect方法中的所有图形。然后,当图形完成时,我从视图中渲染了一个uiimage,并将其与下图合并但是,这种重构仍然不能解决最初的问题,而且这种重构方式使得任何图像擦除功能的实现都更具挑战性。