我在 NSCachesDirectory
中有一个xml文件,当我尝试访问该文件时,如果要从NSBundle访问该文件,则得到零值,而同一个文件则正常工作。屏幕截图和代码片段下方-
// load the data associated to the trackers
- (bool) doLoadTrackersData {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *bookCode = [[NSUserDefaults standardUserDefaults] valueForKey:@"bookCode"];
NSURL *url = [[NSURL fileURLWithPath:cacheDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/%@%s",bookCode,bookCode,bookCode,".xml"]];
NSLog(@"The path for xml ImageTarget is = %@",url.absoluteString);
dataSetStonesAndChips = [self loadObjectTrackerDataSet:url.absoluteString];
dataSetTarmac = [self loadObjectTrackerDataSet:@"Tarmac.xml"];
if ((dataSetStonesAndChips == NULL) || (dataSetTarmac == NULL)) {
NSLog(@"Failed to load datasets");
return false;
}
if (! [self activateDataSet:dataSetStonesAndChips]) {
NSLog(@"Failed to activate dataset");
return false;
}
return true;
}
The path for xml ImageTarget is = file:///var/mobile/Containers/Data/Application/55240D59-8811-4A35-867F-D648197FB611/Library/Caches/AHW2/AHW2/AHW2.xml
本地访问xml文件的代码,并且可以正常工作-
// load the data associated to the trackers
- (bool) doLoadTrackersData {
dataSetStonesAndChips = [self loadObjectTrackerDataSet:@"AHW2.xml"];
dataSetTarmac = [self loadObjectTrackerDataSet:@"Tarmac.xml"];
if ((dataSetStonesAndChips == NULL) || (dataSetTarmac == NULL)) {
NSLog(@"Failed to load datasets");
return false;
}
if (! [self activateDataSet:dataSetStonesAndChips]) {
NSLog(@"Failed to activate dataset");
return false;
}
return true;
}
loadObjectTrackerDataSet
-
// Load the image tracker data set
- (Vuforia::DataSet *)loadObjectTrackerDataSet:(NSString*)dataFile
{
NSLog(@"loadObjectTrackerDataSet (%@)", dataFile);
Vuforia::DataSet * dataSet = NULL;
// Get the Vuforia tracker manager image tracker
Vuforia::TrackerManager& trackerManager = Vuforia::TrackerManager::getInstance();
Vuforia::ObjectTracker* objectTracker = static_cast<Vuforia::ObjectTracker*>(trackerManager.getTracker(Vuforia::ObjectTracker::getClassType()));
if (NULL == objectTracker) {
NSLog(@"ERROR: failed to get the ObjectTracker from the tracker manager");
return NULL;
} else {
dataSet = objectTracker->createDataSet();
if (NULL != dataSet) {
NSLog(@"INFO: successfully loaded data set");
// Load the data set from the app's resources location
if (!dataSet->load([dataFile cStringUsingEncoding:NSASCIIStringEncoding], Vuforia::STORAGE_APPRESOURCE)) {
NSLog(@"ERROR: failed to load data set");
objectTracker->destroyDataSet(dataSet);
dataSet = NULL;
}
}
else {
NSLog(@"ERROR: failed to create data set");
}
}
return dataSet;
}