提取 UI 图像视图坐标
Extracting UI Image View coordinates
我目前正在创建一个基于绘图的应用程序,我希望用户可以选择将坐标发送给另一个玩家。
我在 Xcode 8.1
中使用 swift3
。
我能够提取图像的 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()
}
感谢您的任何想法:)
我建议添加一个数组,您可以在其中添加坐标,然后在您需要的任何地方发送该数组的内容。
例如。像这样:
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()
}
你觉得这有意义吗?
我目前正在创建一个基于绘图的应用程序,我希望用户可以选择将坐标发送给另一个玩家。
我在 Xcode 8.1
中使用 swift3
。
我能够提取图像的 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()
}
感谢您的任何想法:)
我建议添加一个数组,您可以在其中添加坐标,然后在您需要的任何地方发送该数组的内容。 例如。像这样:
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()
}
你觉得这有意义吗?