Ios 自定义磁贴未从“文档”文件夹加载

Ios 自定义磁贴未从“文档”文件夹加载,ios,objective-c,xcode,mktileoverlay,Ios,Objective C,Xcode,Mktileoverlay,我正在使用以下代码将自定义地图分幅加载到我的应用程序中。当我使用mainBundle作为路径时,它会按预期工作: NSString *baseURL = [[[NSBundle mainBundle] bundleURL] absoluteString]; NSString *urlTemplate = [baseURL stringByAppendingString:@"/tiles/{z}/{x}/{y}.png"]; self.tileOverlay = [[MKTileOverlay a

我正在使用以下代码将自定义地图分幅加载到我的应用程序中。当我使用mainBundle作为路径时,它会按预期工作:

NSString *baseURL = [[[NSBundle mainBundle] bundleURL] absoluteString];
NSString *urlTemplate = [baseURL stringByAppendingString:@"/tiles/{z}/{x}/{y}.png"];
self.tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:urlTemplate];
self.tileOverlay.canReplaceMapContent=YES;
[self.mapView insertOverlay:self.tileOverlay belowOverlay:self.gridOverlay];
但是,如果我试图更改文档文件夹的路径,因为我计划下载tiles文件夹并将其存储在documents文件夹中,则以下代码不起作用:

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *urlTemplate = [destinationPath stringByAppendingString:@"tiles/{z}/{x}/{y}.png"];

self.tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:urlTemplate];
self.tileOverlay.canReplaceMapContent=YES;
[self.mapView insertOverlay:self.tileOverlay belowOverlay:self.gridOverlay];
任何暗示都是有用的

注: “平铺”文件夹存在于我的路径上。更具体地说,下面的代码返回YES

NSString* foofile = [destinationPath stringByAppendingPathComponent:@"/tiles/17/70759/49235.png"]; //as an example
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

我刚刚发现为什么代码不起作用

文档路径应为:

NSString *destinationPath = [[self applicationDocumentsDirectory]  absoluteString];
而不是:

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
其中ApplicationDocuments目录为:

 - (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
下面的链接帮助我找到另一种获取文档路径的方法