现在,我使用ARKit 2.0创建了一个应用程序,但是我不知道如何将参考图像放入核心数据中。 因为每次我启动该应用程序时,它都会再次创建参考图像。
var aRIngredients = Set<ARReferenceImage>()
let arImage = ARReferenceImage(((image?.cgImage))!, orientation: CGImagePropertyOrientation.up, physicalWidth: 0.5)
self.ARIngredients.insert(arImage)
configuration.trackingImages = self.ARIngredients
答案 0 :(得分:0)
假设我正确地解释了您的需求,您可以执行以下操作:
首先,我们将在Entity
中创建一个CoreData
,在本示例中将其称为ARCustomImage
,并将有两个attributes
:
BinaryData
)-我们将在其中存储SnapshotImage的地方。String
)-目标图像的唯一ID。确保像这样设置imageData
属性:
然后让我们说我们要将ARSCNView
快照保存到CoreData
。然后我们可以做这样的事情:
/// Saves The Data Needed To Store Our ARReferenceImage
@IBAction func saveReferenceImage(){
//1. Take A SnapShot Of The ARSCNView
let screenShot = self.augmentedRealityView.snapshot()
//2. Save To CoreData
if let arImageEntry = NSEntityDescription.entity(forEntityName: ENTITY_NAME, in: databaseContext),
let newCustomARImage = NSManagedObject(entity: arImageEntry, insertInto: databaseContext) as? ARCustomImage{
//a. Set The Target Name Of Our ARReferenceImage
newCustomARImage.name = UUID().uuidString
//b. Store Our Image To The Stack
newCustomARImage.imageData = UIImagePNGRepresentation(screenShot)
do {
try databaseContext.save()
//c. Load The Custom Images Again So We Can Start Our Configuration With Custom Images
loadExistingReferenceImages()
} catch {
print("Error Saving \(UUID().uuidString) ")
}
}else{
print("Error Generation \(UUID().uuidString) ")
}
}
然后我们可以检查是否有保存的数据,然后将它们转换为Set
的{{1}},如下所示:
ARReferenceImage
我没有注释每个部分,而是注释了所有代码,因此它应该是完全可以自我解释的(这里是完整的),前提是您当然可以通过默认设置来设置CoreData:
/// Loads All Our ARReferenceImages From CoreData
func loadExistingReferenceImages(){
var trackingImages = [ARReferenceImage]()
//1. Generate A Fetch Request For Our Custom ARImages
let request = NSFetchRequest<NSFetchRequestResult>(entityName: ENTITY_NAME)
request.returnsObjectsAsFaults = false
do {
//2. Get Our Saved Data
let savedData = try databaseContext.fetch(request) as! [ARCustomImage]
//3. Loop Through Them & Create Our ARReferenceImage
for customImage in savedData {
//b. Get The Target Name & Image Back From Our Data & Convert To A CGImage
if let targetName = customImage.name,
let image = UIImage(data: customImage.imageData!),
let arCGImage = image.cgImage {
//c. Get The Width Of Our Image
let widthInCM: CGFloat = CGFloat(arCGImage.width) / CGFloat(47)
let widthInMetres: CGFloat = widthInCM * 0.01
//d. Create An ARReferenceImage
let arReferenceImage = ARReferenceImage(arCGImage,
orientation: cgImagePropertyOrientation(image.imageOrientation),
physicalWidth: widthInMetres)
arReferenceImage.name = targetName
//e. Add It To Our Tracking Images Array
trackingImages.append(arReferenceImage)
print("""
ARReferenceGenerated
Name = \(arReferenceImage.name!)
Width = \(arReferenceImage.physicalSize.width)
""")
}
}
} catch { print ("Fatal Error Loading Data") }
//4. Set Our ARSession Configuration Detection Images Using The Images From CoreData
if !trackingImages.isEmpty{
print("Loading An Existing Total Of \(trackingImages.count) ARReference Images")
configuration.detectionImages = Set(trackingImages)
augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors ])
}else{
//2. Run A Standard Session
print("No ARReference Images Have Been Saved To CoreData")
augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}
}
请注意,这只是一个非常粗糙的工作示例,并未以任何形状或形式进行优化。
话虽如此,它肯定会为您指明正确的方向...