【问题标题】:ARKit add reference images from urlARKit 从 url 添加参考图像
【发布时间】:2019-03-06 15:44:10
【问题描述】:

我想从服务器添加 ARReferenceImages。为此,我得到一个包含图像 URL 的数组。 T 尝试使用以下函数,该函数迭代 url 并将新创建的 ARImages 添加到集合中。但它不起作用。

    func addReferences(media: NSArray){
    var customReferenceSet = Set<ARReferenceImage>()
    for medium in media{
        let type = (medium as AnyObject).value(forKey: "type");
        let t = type as! String

        let reference = (medium as AnyObject).value(forKey: "reference");
        let ref = reference as! String


        let ide = (medium as AnyObject).value(forKey: "id");
        let id = ide as! String


        let url = URL(string: ref)
        let session = URLSession(configuration: .default)


        let downloadPicTask = session.dataTask(with: url!) { (data, response, error) in

            if let e = error {
                print("Error downloading picture: \(e)")
            } else {

                if let res = response as? HTTPURLResponse {
                    print("Downloaded picture with response code \(res.statusCode)")
                    if let imageData = data {
                        let image = UIImage(data: imageData)!
                        let imageToCIImage = CIImage(image: image)
                        let cgImage = self.convertCIImageToCGImage(inputImage: imageToCIImage!)
                        let arImage = ARReferenceImage(cgImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: 0.2)
                        arImage.name = id
                        customReferenceSet.insert(arImage)
                    } else {
                        print("Couldn't get image: Image is nil")
                    }
                } else {
                    print("Couldn't get response code for some reason")
                }
            }
        }

        downloadPicTask.resume()
    }

    self.configuration = ARWorldTrackingConfiguration()
    self.configuration?.detectionImages = customReferenceSet
    sceneView.session.run(configuration!)
}

有时,但不是每次,我都会得到以下输出:

[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接已通过 close_notify 警报彻底关闭

一些想法?

【问题讨论】:

    标签: ios swift arkit image-recognition


    【解决方案1】:

    问题是您在异步网络请求实际获取图像之前设置了detectionImages,这意味着您实际上将空集分配给了detectionImages

    您需要确保仅在从服务器获取所有图像后才设置detectionImages,您可以使用DispatchGroup 来实现。

    func addReferences(media: NSArray){
        var customReferenceSet = Set<ARReferenceImage>()
        let imageFetchingGroup = DispatchGroup()
        for medium in media { 
            let type = (medium as AnyObject).value(forKey: "type");
            let t = type as! String
    
            let reference = (medium as AnyObject).value(forKey: "reference");
            let ref = reference as! String
    
            let ide = (medium as AnyObject).value(forKey: "id");
            let id = ide as! String
    
            let url = URL(string: ref)
            let session = URLSession(configuration: .default)
    
            imageFetchingGroup.enter()
            let downloadPicTask = session.dataTask(with: url!) { (data, response, error) in
                if let e = error {
                    print("Error downloading picture: \(e)")
                    imageFetchingGroup.leave()
                } else {
                    if let res = response as? HTTPURLResponse {
                        print("Downloaded picture with response code \(res.statusCode)")
                        if let imageData = data {
                            let image = UIImage(data: imageData)!
                            let arImage = ARReferenceImage(image.cgImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: 0.2)
                            arImage.name = id
                            customReferenceSet.insert(arImage)
                            imageFetchingGroup.leave()
                        } else {
                            print("Couldn't get image: Image is nil")
                            imageFetchingGroup.leave()
                        }
                    } else {
                        print("Couldn't get response code for some reason")
                        imageFetchingGroup.leave()
                    }
                }
            }
    
            downloadPicTask.resume()
        }
    
        self.configuration = ARWorldTrackingConfiguration()
        imageFetchingGroup.notify(queue: .main) {
            self.configuration?.detectionImages = customReferenceSet
            sceneView.session.run(configuration!)
        }
    }
    

    您也不应该在 Swift 中使用 NSArray,尤其不应该将具有已知属性的类型转换为 AnyObject,只是为了使用带有静态键的 value(forKey:) 动态检索它们的属性。只需将Array 与具体类型一起使用,并使用点语法访问属性。

    也不需要从 UIImageCIImage 再到 CGImage 的自定义转换,UIImage 有一个内置属性 cgImage 可用于转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2018-08-06
      • 2020-10-05
      • 2018-07-30
      • 2023-03-10
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多