CachedTileOverlay.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // CachedTileOverlay.m
  3. // Apex Mobile
  4. //
  5. // Created by Rui Zhang on 12/31/19.
  6. // Copyright © 2019 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CachedTileOverlay.h"
  9. #import "FileCache.h"
  10. @implementation CachedTileOverlay
  11. - (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData * _Nullable, NSError * _Nullable))result
  12. {
  13. if (!result)
  14. {
  15. return;
  16. }
  17. NSURL* url=[self URLForTilePath:path];
  18. NSString* img_url = [url absoluteString];
  19. NSString* file_name=[img_url lastPathComponent];
  20. NSData *cachedData = [FileCache load_cached_img:file_name loadFrom:img_url];
  21. if (cachedData)
  22. {
  23. result(cachedData, nil);
  24. }
  25. else
  26. {
  27. NSLog(@"load image");
  28. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  29. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  30. NSError* error;
  31. NSData* downloadimg_data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
  32. dispatch_async(dispatch_get_main_queue(), ^{
  33. if(downloadimg_data!=nil)
  34. {
  35. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  36. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  37. // cell.cellImageView.image = img;
  38. }
  39. result(downloadimg_data, error);
  40. // else
  41. // cell.cellImageView.image = [UIImage imageNamed:notFound];
  42. });
  43. });
  44. // NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
  45. // [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  46. // // Should inspect the response to see if the request completed successfully!!
  47. // [self.cache setObject:data forKey:[self URLForTilePath:path]];
  48. // result(data, connectionError);
  49. // }];
  50. }
  51. //
  52. // NSLog(@"%@",url);
  53. //// NSLog(@"%ld,%ld,%ld",path.x,path.y,path.z);
  54. // [super loadTileAtPath:path result:result];
  55. }
  56. @end