// // CachedTileOverlay.m // Apex Mobile // // Created by Rui Zhang on 12/31/19. // Copyright © 2019 United Software Applications, Inc. All rights reserved. // #import "CachedTileOverlay.h" #import "FileCache.h" @implementation CachedTileOverlay - (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData * _Nullable, NSError * _Nullable))result { if (!result) { return; } NSURL* url=[self URLForTilePath:path]; NSString* img_url = [url absoluteString]; NSString* file_name=[img_url lastPathComponent]; NSData *cachedData = [FileCache load_cached_img:file_name loadFrom:img_url]; if (cachedData) { result(cachedData, nil); } else { NSLog(@"load image"); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]]; NSError* error; NSData* downloadimg_data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error]; dispatch_async(dispatch_get_main_queue(), ^{ if(downloadimg_data!=nil) { [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url]; // UIImage * img =[UIImage imageWithData:downloadimg_data]; // cell.cellImageView.image = img; } result(downloadimg_data, error); // else // cell.cellImageView.image = [UIImage imageNamed:notFound]; }); }); // NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]]; // [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // // Should inspect the response to see if the request completed successfully!! // [self.cache setObject:data forKey:[self URLForTilePath:path]]; // result(data, connectionError); // }]; } // // NSLog(@"%@",url); //// NSLog(@"%ld,%ld,%ld",path.x,path.y,path.z); // [super loadTileAtPath:path result:result]; } @end