使用Pickerview中的数据更改ARKit中的图像

时间:2019-01-03 08:37:59

标签: swift xcode uipickerview arkit

我在自己的应用中添加了pickerview,以更改ARKit(可检测墙壁)的可放置图像。 我第一次尝试使用8个不同的ARScenekits成功,但是我想使用1个Scenekit和一个pickerview来更改图像。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var picker: UIPickerView!

var pickerData: [String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Connect data:
    self.picker.delegate = self
    self.picker.dataSource = self

    // Input the data into the array
    pickerData = ["GT2W", "GT4DW", "GT6W", "GT8W", "GT10DW", "GT12W"]
}

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Number of columns of data
func numberOfComponents(in pickerView: UIPickerView) ->; Int {
    return 1
}

// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->; Int {
    return pickerData.count
}

// The data to return fopr the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) ->; String? {
    return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // This method is triggered whenever the user makes a change to the picker selection.
    // The parameter named row and component represents what was selected.
}

我将所选的类型保存在var pickerData中,我想使用此var选择正确的图像。 enter image description here

添加此功能后,我更改了选择图像的代码: 我改变了

  

material.diffuse.contents = pickerData // UIImage(名称:“ GT2W”)

但是结果是,仅显示一个白色字段。 我用于pickerData的名称与图像名称相同。

我做错了什么?

这是我的“放置”代码的其余部分:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor, planeAnchor.alignment == .vertical else { return }
let grid = Grid(anchor: planeAnchor)
self.grids.append(grid)
node.addChildNode(grid)
}

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor, planeAnchor.alignment == .vertical else { return }
let grid = self.grids.filter { grid in
    return grid.anchor.identifier == planeAnchor.identifier
    }.first

guard let foundGrid = grid else {
    return
}

foundGrid.update(anchor: planeAnchor)
}

@objc func tapped(gesture: UITapGestureRecognizer) {
// Get 2D position of touch event on screen
let touchPosition = gesture.location(in: sceneView)

// Translate those 2D points to 3D points using hitTest (existing plane)
let hitTestResults = sceneView.hitTest(touchPosition, types: .existingPlaneUsingExtent)

// Get hitTest results and ensure that the hitTest corresponds to a grid that has been placed on a wall
guard let hitTest = hitTestResults.first, let anchor = hitTest.anchor as? ARPlaneAnchor, let gridIndex = grids.index(where: { $0.anchor == anchor }) else {
    return
}
addPainting(hitTest, grids[gridIndex])
}

func addPainting(_ hitResult: ARHitTestResult, _ grid: Grid) {
// 1.
let planeGeometry = SCNPlane(width: 0.1, height: 0.1)
let material = SCNMaterial()
material.diffuse.contents = pickerData //UIImage(named: "GT2W")
planeGeometry.materials = [material]

// 2.
let paintingNode = SCNNode(geometry: planeGeometry)
paintingNode.transform = SCNMatrix4(hitResult.anchor!.transform)
paintingNode.eulerAngles = SCNVector3(paintingNode.eulerAngles.x + (-Float.pi / 2), paintingNode.eulerAngles.y, paintingNode.eulerAngles.z)
paintingNode.position = SCNVector3(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)

sceneView.scene.rootNode.addChildNode(paintingNode)
grid.removeFromParentNode()
}
}

1 个答案:

答案 0 :(得分:1)

您将需要在选择器中处理选择

func pickerView(_ pickerView: UIPickerView, 
            didSelectRow row: Int, 
             inComponent component: Int) {

   let pickerValue = pickerData[row]
   let image = UIImage(named: pickerValue)
   // do something with the selected image
   material.diffuse.contents = image
}

编辑:

因为要在轻击手势中获取图像,所以我们需要一个默认图像,并且需要保留对最后选择的图像的引用。因此,在首次加载图片时,如果用户尚未手动选择图片,则默认使用列表中的第一张图片

var defaultImage: UIImage {
    return UIImage(named: pickerData[0]) // just select first value
}

var selectedImage: UIImage? 

func pickerView(_ pickerView: UIPickerView, 
            didSelectRow row: Int, 
             inComponent component: Int) {

   let pickerValue = pickerData[row]
   selectedImage = UIImage(named: pickerValue)
}

然后在当前点击的功能中,编辑设置图像的行:

@objc func tapped(gesture: UITapGestureRecognizer) {
     // ... previous code 
     material.diffuse.contents = selectedImage ?? defaultImage
}