我正在学习使用 ARKIT,我想知道是否有一种方法可以从应用程序内添加参考图像(要识别的图像)(基于用户的选择)。根据文档,这可以通过将参考图像添加到资产(在开发阶段)来完成,这限制了应用程序的可用性。我想知道是否有一种方法可以根据用户的选择下载/添加这些图像并将这些图像用作参考图像(在应用程序内)。
如果您查看ARReferenceImage的文档,您会注意到有两种手动生成 ARReferenceImages 的方法:
\n\ninit(CGImage, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)\ninit(CVPixelBuffer, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)\nRun Code Online (Sandbox Code Playgroud)\n\n如果您从服务器下载,则需要第一个,它需要使用CGImage.
因此,您下载的任何图像都需要使用此方法进行转换。
\n\n要首先从服务器下载图像,您需要使用 URLSession将这些图像下载到设备上的某个位置,例如Documents Directory.
一个简单的例子如下:
\n\n/// Downloads An Image From A Remote URL\nfunc downloadImageTask(){\n\n //1. Get The URL Of The Image\n guard let url = URL(string: "http://www.blackmirrorz.tech/images/BlackMirrorz/blackMirrorzLogo.png") else { return }\n\n //2. Create The Download Session\n let downloadSession = URLSession(configuration: URLSession.shared.configuration, delegate: self, delegateQueue: nil)\n\n //3. Create The Download Task & Run It\n let downloadTask = downloadSession.downloadTask(with: url)\n downloadTask.resume() \n }\n\n }\nRun Code Online (Sandbox Code Playgroud)\n\n创建 URLSession 后,您需要注册URLSessionDownloadDelegate以下方法:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)\nRun Code Online (Sandbox Code Playgroud)\n\n其中location参数指的是:
\n\n\n临时文件的文件 URL。由于该文件是临时文件,因此您必须打开该文件进行读取,或者将其移动到 app\xe2\x80\x99s 沙箱容器目录中的永久位置,然后再从此委托方法返回。
\n
因此,您的回调可能如下所示:
\n\nfunc urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {\n\n //1. Create The Filename\n let fileURL = getDocumentsDirectory().appendingPathComponent("image.png")\n\n //2. Copy It To The Documents Directory\n do {\n try FileManager.default.copyItem(at: location, to: fileURL)\n\n print("Successfuly Saved File \\(fileURL)")\n\n } catch {\n\n print("Error Saving: \\(error)")\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我使用以下函数来获取用户Documents Directory:
/// Returns The Documents Directory\n///\n/// - Returns: URL\nfunc getDocumentsDirectory() -> URL {\n\nlet paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)\nlet documentsDirectory = paths[0]\nreturn documentsDirectory\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在我们已经下载了图像,然后我们将创建一个函数来检索这些图像并返回一组ARReferenceImage所需的图像ARWorldTrackingConfiguration一组图像。
/// Creates A Set Of ARReferenceImages From All PNG Content In The Documents Directory\n///\n/// - Returns: Set<ARReferenceImage>\nfunc loadedImagesFromDirectoryContents() -> Set<ARReferenceImage>?{\n\n var index = 0\n var customReferenceSet = Set<ARReferenceImage>()\n let documentsDirectory = getDocumentsDirectory()\n\n do {\n\n let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])\n\n let filteredContents = directoryContents.filter{ $0.pathExtension == "png" }\n\n filteredContents.forEach { (url) in\n\n do{\n\n //1. Create A Data Object From Our URL\n let imageData = try Data(contentsOf: url)\n guard let image = UIImage(data: imageData) else { return }\n\n //2. Convert The UIImage To A CGImage\n guard let cgImage = image.cgImage else { return }\n\n //3. Get The Width Of The Image\n let imageWidth = CGFloat(cgImage.width)\n\n //4. Create A Custom AR Reference Image With A Unique Name\n let customARReferenceImage = ARReferenceImage(cgImage, orientation: CGImagePropertyOrientation.up, physicalWidth: imageWidth)\n customARReferenceImage.name = "MyCustomARImage\\(index)"\n\n //4. Insert The Reference Image Into Our Set\n customReferenceSet.insert(customARReferenceImage)\n\n print("ARReference Image == \\(customARReferenceImage)")\n\n index += 1\n\n }catch{\n\n print("Error Generating Images == \\(error)")\n\n }\n\n }\n\n } catch {\n\n print("Error Reading Directory Contents == \\(error)")\n\n }\n\n //5. Return The Set\n return customReferenceSet\n}\nRun Code Online (Sandbox Code Playgroud)\n\n因此,要将最后一个函数落实到位,您需要执行以下操作:
\n\nlet detectionImages = loadedImagesFromDirectoryContents()\n\nconfiguration.detectionImages = detectionImages\n\naugmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors])\nRun Code Online (Sandbox Code Playgroud)\n\n希望能帮助到你...
\n| 归档时间: |
|
| 查看次数: |
1742 次 |
| 最近记录: |