使用 iOS - Swift Xcode 14 我使用一些点绘制了图像......并且我得到了图像作为输出。但该图像是倒置或翻转的图像。 我使用了几个代码来翻转/反转图像,但没有一个代码起作用。 通过使用一些代码,结果图像会超出屏幕。 这里提供的代码是 90% 正确的代码,但图像被翻转/反转。
import SceneKit
class StairTwoDSceneViewController: UIViewController {
var plotPointsArray: [[Double]] = [[0.0,0.0,0.0], [0.229,0.0,0.005], [0.229,-0.0249,0.1926], [0.4566,-0.0249,0.2073], [0.4341,-0.0315,0.3989],[0.6754,-0.0315,0.4039],[0.6513,-0.0315,0.5892], [0.8782,-0.0315,0.5971], [0.8541,-0.0315,0.7834],[1.0765,-0.0315,0.78781]]
let lineWidth: CGFloat = 0.1 // Adjust this value to increase or decrease line thickness
let cornerRadius: CGFloat = 0.02 // Adjust this value to control the size of the corner circles
override func viewDidLoad() {
super.viewDidLoad()
let sceneView = SCNView(frame: view.bounds)
sceneView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(sceneView)
let scene = SCNScene()
if plotPointsArray.count > 0 {
let pointsNode = SCNNode()
for (index, point) in plotPointsArray.enumerated() {
guard point.count == 2 || point.count == 3 else {
continue // Skip invalid points
}
// Adjust the y-coordinate to place the green sphere at the left bottom
let spherePosition = SCNVector3(
x: Float(point[0]),
y: Float(point.count == 2 ? -0.0315 : -point[1]), // Flip and adjust y-coordinate
z: Float(point[point.count - 1])
)
let sphereNode = drawSphere(at: spherePosition, radius: cornerRadius, color: index == 0 ? UIColor.green : UIColor.red)
pointsNode.addChildNode(sphereNode)
}
scene.rootNode.addChildNode(pointsNode)
for i in 0..<plotPointsArray.count - 1 {
let currentPoint = plotPointsArray[i]
let nextPoint = plotPointsArray[i + 1]
guard currentPoint.count == nextPoint.count else {
continue // Skip invalid points
}
let start = SCNVector3(currentPoint[0], currentPoint.count == 2 ? -0.0315 : -currentPoint[1], currentPoint[currentPoint.count - 1])
let end = SCNVector3(nextPoint[0], nextPoint.count == 2 ? -0.0315 : -nextPoint[1], nextPoint[nextPoint.count - 1])
let lineGeometry = SCNGeometry.lineGeometry(from: start, to: end)
let lineNode = SCNNode(geometry: lineGeometry)
scene.rootNode.addChildNode(lineNode)
}
}
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.eulerAngles.x = -Float.pi / 2
cameraNode.eulerAngles.y = 0
cameraNode.position = SCNVector3(0.5, 2.5, 0.5)
scene.rootNode.addChildNode(cameraNode)
sceneView.scene = scene
sceneView.backgroundColor = UIColor.black
sceneView.allowsCameraControl = true
}
func drawLine(from start: SCNVector3, to end: SCNVector3, thickness: CGFloat, color: UIColor) -> SCNNode {
let lineGeometry = SCNGeometry.lineGeometry(from: start, to: end)
lineGeometry.firstMaterial?.diffuse.contents = color
let lineNode = SCNNode(geometry: lineGeometry)
lineNode.position = SCNVector3((start.x + end.x) / 2, (start.y + end.y) / 2, (start.z + end.z) / 2)
lineNode.eulerAngles.z = -atan2(end.x - start.x, end.y - start.y)
return lineNode
}
func drawSphere(at position: SCNVector3, radius: CGFloat, color: UIColor) -> SCNNode {
let sphereGeometry = SCNSphere(radius: radius)
let sphereNode = SCNNode(geometry: sphereGeometry)
sphereNode.position = position
sphereNode.geometry?.firstMaterial?.diffuse.contents = color
return sphereNode
}
}
extension SCNGeometry {
static func lineGeometry(from start: SCNVector3, to end: SCNVector3) -> SCNGeometry {
let indices: [Int32] = [0, 1]
let source = SCNGeometrySource(vertices: [start, end])
let element = SCNGeometryElement(indices: indices, primitiveType: .line)
return SCNGeometry(sources: [source], elements: [element])
}
}
extension UIImage {
func rotate(degrees: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
context.rotate(by: degrees * CGFloat.pi / 180)
self.draw(in: CGRect(origin: .zero, size: self.size))
let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return rotatedImage
}
}
注意:不要更改积分,积分是从后端获取的。我在这里使用了样本点
我需要翻转/反转图像。
使用您生成的“楼梯台阶”,相机从 Y 角度 观察 ...“楼梯”在 Z 角度下降。
根据您想要做什么,您可以翻转相机:
cameraNode.eulerAngles.x = -Float.pi / 2
//cameraNode.eulerAngles.y = 0
cameraNode.eulerAngles.y = -Float.pi / 2
或者,平移 Z 坐标以反转楼梯:
if plotPointsArray.count > 0 {
let pointsNode = SCNNode()
// only use elements that have x,y,z values
plotPointsArray.removeAll(where: {$0.count != 3})
// will never fail (because we're only here if .count > 0)
// but safely unwrap anyway
guard let lastPoint = plotPointsArray.last
else { return }
// assuming for now the z-values are from 0.0 to a positive value
let maxZ = lastPoint[2]
for (index, point) in plotPointsArray.enumerated() {
// Adjust the Z-coordinate to place the green sphere at the left bottom
let spherePosition = SCNVector3(
x: Float(point[0]),
y: Float(point[1]),
z: Float(point[2] - maxZ)
)
let sphereNode = drawSphere(at: spherePosition, radius: cornerRadius, color: index == 0 ? UIColor.green : UIColor.red)
pointsNode.addChildNode(sphereNode)
}
scene.rootNode.addChildNode(pointsNode)
// let's use those sphere nodes to generate the
// "connecting lines" instead of re-converting the plotPointsArray
var startVect: SCNVector3 = pointsNode.childNodes[0].position
for (i, n) in pointsNode.childNodes.enumerated() {
// skip the first time through
// since we have only the starting point
guard i != 0 else {
continue
}
let nextVect = n.position
let lineGeometry = SCNGeometry.lineGeometry(from: startVect, to: nextVect)
let lineNode = SCNNode(geometry: lineGeometry)
scene.rootNode.addChildNode(lineNode)
startVect = nextVect
}
}