我正在创建一个基于绘图的应用程序,我希望用户可以选择将坐标发送给另一个玩家。
我在swift3
中使用Xcode 8.1
。
我能够提取图像的PNG
,但我想要做的只是发送坐标以便在其他玩家屏幕上重新创建图像。
我已将代码发布到用户的绘图功能。我确实试图推动'背景? '在'addLine'之后进入一个数组,但看起来像坐标的东西都没有被推入数组。
func drawPicture(fromPoint:CGPoint, toPoint:CGPoint) {
UIGraphicsBeginImageContextWithOptions(self.drawPage.bounds.size, false, 0.0)
drawPage.image?.draw(in: CGRect(x: 0, y:0, width:self.drawPage.bounds.width, height:self.drawPage.bounds.height))
let context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
context?.setBlendMode(CGBlendMode.color)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(5)
context?.setStrokeColor(UIColor(red: 0.26, green: 0.53, blue: 0.96, alpha: 1.0).cgColor)
context?.strokePath()
drawPage.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
感谢您的任何想法:)
答案 0 :(得分:0)
我建议添加一个数组,在绘图时添加坐标,然后只需要在任何地方发送该数组的内容。 例如。像这样的东西:
struct DrawingCoordinate {
var from: CGPoint
var to: CGPoint
init(from: CGPoint, to: CGPoint) {
self.from = from
self.to = to
}
}
var coordinatesArray = [DrawingCoordinate]()
func drawPicture(fromPoint:CGPoint, toPoint:CGPoint) {
UIGraphicsBeginImageContextWithOptions(self.drawPage.bounds.size, false, 0.0)
drawPage.image?.draw(in: CGRect(x: 0, y:0, width:self.drawPage.bounds.width, height:self.drawPage.bounds.height))
let context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
// add this line to your array
coordinatesArray.append(DrawingCoordinate(from: fromPoint, to: toPoint))
context?.setBlendMode(CGBlendMode.color)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(5)
context?.setStrokeColor(UIColor(red: 0.26, green: 0.53, blue: 0.96, alpha: 1.0).cgColor)
context?.strokePath()
drawPage.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
这对你有意义吗?