【发布时间】:2017-11-28 15:07:17
【问题描述】:
我正在尝试开发一个可以在 UIImageView 上绘制内容的应用程序。我有以下代码。
import UIKit
class Line
{
var startPoint:CGPoint
var endPoint:CGPoint
init (start:CGPoint , end:CGPoint)
{
startPoint = start
endPoint = end
}
}
class AnnotationView: UIView {
static internal let nibName = "AnnotationView"
@IBOutlet weak var imageView: UIImageView!
var lines :[Line] = []
var lastPoint:CGPoint!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lastPoint = touches.first?.location(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first
{
let newPoint = touch.location(in:self)
lines.append(Line(start: lastPoint, end: newPoint))
lastPoint = newPoint
self.setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext()
{
context.beginPath()
for line in lines
{
context.move(to: line.startPoint)
context.addLine(to: line.endPoint)
}
context.setLineCap(CGLineCap.round)
context.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1)
context.setLineWidth(5)
context.strokePath()
}
}
}
所以现在在上面的代码中,我有一个派生自 UIView 的 AnnotationView 类,其中我有一个 UIImageView,我从界面构建器将其设置为超级视图的完整大小。这是一个简单的绘图应用程序,我想在 UIImageView 上绘制一些东西。现在的问题是当 imageView.image 为 nil 时,它会显示我的绘图,如果为 imageView.image 设置了任何图像,那么它不会显示绘图。它只显示在此 imageView 中设置的图像。我在这两种情况下都调试了所有方法都被调用。肯定有我想念的东西。谁能帮帮我吗?我想在我在 UIImageView 中设置的图像上显示我的绘图。 问候, 尼娜
【问题讨论】:
标签: swift uiimageview drawing core-graphics drawrect