自定义切片未从Documents文件夹加载

时间:2018-01-24 08:04:14

标签: ios objective-c xcode mktileoverlay

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

MONTHS A_NEW A_OLD B_NEW B_OLD C_NEW C_OLD D_NEW D_OLD
------+-----+-----+-----+-----+-----+-----+-----+-----
JAN    0     1     0     0     1     0     0     0
FEB    0     1     0     0     0     0     0     1
MAR    0     0     0     0     1     0     1     0
APR    0     0     0     0     0     0     0     1
MAY    0     1     0     0     0     0     0     0
JUN    0     0     0     0     0     1     1     0
JUL    0     0     0     0     0     0     0     0
AUG    0     2     0     0     0     0     1     0
SEP    0     1     0     0     0     0     0     1  
OCT    0     0     0     0     0     0     0     0
NOV    0     0     0     0     0     0     0     0
DEC    1     0     0     2     0     1     0     1

但是如果我尝试更改文档文件夹的路径(因为我打算下载tiles文件夹并将它们存储在文档文件夹中),则以下代码无效:

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文件夹存在于我的路径上。更具体地说,以下代码返回YES

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];

1 个答案:

答案 0 :(得分:1)

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

文件路径应为:

NSString *destinationPath = [[self applicationDocumentsDirectory]  absoluteString];

而不是:

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

applicationDocumentsDirectory是:

 - (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

以下链接https://stackoverflow.com/a/34543841/1465756帮助我找到另一种获取文档路径的方法。