ARKit从URL添加参考图像

时间:2018-10-01 13:52:19

标签: ios swift arkit image-recognition

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

    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警报完全关闭了连接

一些想法?

1 个答案:

答案 0 :(得分: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可以用于转换。