gpt4 book ai didi

ios - ARKit从URL添加引用图像

转载 作者:行者123 更新时间:2023-12-01 19:42:55 25 4
gpt4 key购买 nike

我想从服务器添加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警报完全关闭了连接

有什么想法吗?

最佳答案

问题是您在异步网络请求实际获取图像之前设置了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,可用于转换。

关于ios - ARKit从URL添加引用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52592675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com