Search code examples
swiftxcodescenekitarkitdetection

How to modify "AR Reference Image Set" during session run?


In my app I take several photos which I add as UIImages to an array called imagesPicked. I convert that array to a Set which I load by a button action to a configuration and then I run a session by resetTracking(). All that by following code. But I want to modify that code to be able to add new images to AR Reference Images Set (e.g. to change the configuration) while still running, without reseting the session. How can I achieve that? Thx.

var detectionImages = Set<ARReferenceImage>()
func resetTracking() {
        let configuration = ARWorldTrackingConfiguration()
        configuration.detectionImages = detectionImages
        session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
    } 


var index = 0
 func createARReferenceSet() -> Set<ARReferenceImage>?
{
        var customReferenceSet = Set<ARReferenceImage>()
        do{
            for i in 0...imagesPicked.count-1{
                let image = imagesPicked[i]
                let cgImage = image?.cgImage
                let imageWidth = CGFloat(0.1)
                let customARReferenceImage = ARReferenceImage(cgImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: imageWidth)
                customARReferenceImage.name = "MyCustomARImage\(index)"
                customReferenceSet.insert(customARReferenceImage)
                index += 1
            }
            }
        return customReferenceSet
    }
}


@IBAction func detectChosenImage(_ sender: Any) {
        detectionImages = createARReferenceSet()!
        resetTracking()
    }

Solution

  • In order to add images to the AR Reference Images Set without resetting current session run a new configuration without [.resetTracking, .removeExistingAnchors] options, try just calling session.run(configuration).