FileCache.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // FileCache.m
  3. // RedAnt Mobile
  4. //
  5. // Created by Ray on 08/09/2017.
  6. // Copyright © 2017 Ray. All rights reserved.
  7. //
  8. #import "FileCache.h"
  9. #import "AppDelegate.h"
  10. #import "const.h"
  11. #import "RAUtils.h"
  12. @implementation FileCache
  13. + (void) cache_img: (NSData*) imgData filename:(NSString*) name saveTo:(NSString*) path
  14. {
  15. path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  16. path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  17. path=[path stringByReplacingOccurrencesOfString:name withString:@""];
  18. if(path.length==0)
  19. path=@"";
  20. if(imgData==nil)
  21. return;
  22. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  23. NSString *cachefolder = [paths objectAtIndex:0];
  24. NSString *img_cache = [cachefolder stringByAppendingPathComponent:[NSString stringWithFormat:@"img_cache/%@",path]];
  25. NSFileManager* fileManager = [NSFileManager defaultManager];
  26. BOOL bdir=YES;
  27. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  28. {
  29. NSError *error = nil;
  30. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  31. if(!bsuccess)
  32. DebugLog(@"Create cache folder failed");
  33. }
  34. NSString *filePath = [img_cache stringByAppendingPathComponent:name];
  35. bool bsuccess=[imgData writeToFile:filePath atomically:YES];
  36. if(bsuccess)
  37. DebugLog(@"IMG CACHE SUCCESS,%@",name);
  38. else
  39. DebugLog(@"IMG CACHE FAILED,%@",name);
  40. }
  41. + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
  42. {
  43. if(path.length==0)
  44. return nil;
  45. path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  46. path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  47. // path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
  48. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  49. // if(appDelegate.bEnable_Cache==false)
  50. // return nil;
  51. NSData* data = nil;
  52. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  53. NSString *cachefolder = [paths objectAtIndex:0];
  54. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  55. NSString *filePath = [img_cache stringByAppendingPathComponent:path];
  56. NSFileManager* fileManager = [NSFileManager defaultManager];
  57. if( [fileManager fileExistsAtPath:filePath ])
  58. {
  59. data = [NSData dataWithContentsOfFile: filePath];
  60. }
  61. // NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT img FROM img_cache WHERE name = '%@'", filename];
  62. // sqlite3_stmt* statement;
  63. // sqlite3 *db = [self get_db];
  64. //
  65. //// if( sqlite3_prepare_v2(db, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
  66. //// {
  67. //// if( sqlite3_step(statement) == SQLITE_ROW )
  68. //// {
  69. //// int length = sqlite3_column_bytes(statement, 0);
  70. //// data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
  71. //// }
  72. //// }
  73. //
  74. // // Finalize and close database.
  75. // sqlite3_finalize(statement);
  76. // [iSalesDB close_db:db];
  77. return data;
  78. }
  79. @end