Search code examples
iosswiftaugmented-realityarkit

AR Kit 2.0 tracking images from the server


I recently built out a working AR application that deals with the creation of your own custom AR objects with your own photos and text.

Right now the app generates a QR code and stores that QR code image on a server. I wanted to make something that can retrieve these images and store them into the AR Resource Group folder. Currently what I do is actually download all the images, converting them to ARReferenceImage type then running the session with the list of these images. As my app gets more users and creations the download is taking a much longer time.

code to retrieving and converting all image types

func getAllQRCodes(completion: @escaping CompletionHandler) {
            var downloaded = 0
            let reference = Database.database().reference().child("QR")
            reference.observe(DataEventType.value, with: { (snapshot) in
                let postDict = snapshot.value as? [String : String] ?? [:]
                DispatchQueue.global(qos: .background).async {
                    for (key, value) in postDict {
                        print("downloaded: \(downloaded) / total: \(postDict.count)")
                        let percentage = (Double(downloaded)/Double(postDict.count)) * 100
                        DispatchQueue.main.async {
                            self.qrProgressCircle.startProgress(to: CGFloat(percentage), duration: 1)
                        }
                        if self.downloadURLs.contains(value) == false {
                            self.downloadURLs.insert(value)
                            let qrImage = UIImage(url: URL(string: value))
                            let qrCiImage = CIImage(image: qrImage!)
                            let qrCGImage = self.convertCIImageToCGImage(inputImage: qrCiImage!)
                            let qrARImage = ARReferenceImage(qrCGImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: 0.2)
                            qrARImage.name = key
                            self.ARTrackingImages.insert(qrARImage)
                            downloaded += 1
                        }
                    }
                completion(true)
                }
            })
    }

My question is, is there a better way to do this? Perhaps running it on a background thread then constantly rerunning sceneView session? (Would that actually result in horrible user experience?)


Solution

  • Instead of downloading all the images from the server I actually detect the images first from the session delegate function didUpdate frame

    From the frame I get I then make the correct operations to add the image to my AR Resource group.