FileCache.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  19. // NSString *cachefolder = [paths objectAtIndex:0];
  20. // NSString *img_cache = [cachefolder stringByAppendingPathComponent:[NSString stringWithFormat:@"img_cache/%@",@"www.newpacificdirect.com/u/NPD/20160615/mytest/"]];
  21. // if ([[NSFileManager defaultManager] fileExistsAtPath:img_cache]) {
  22. //
  23. // DebugLog(@"目录已经存在了");
  24. //
  25. // }
  26. // else
  27. // {
  28. // NSError *error = nil;
  29. // bool bsuccess=[[NSFileManager defaultManager] createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  30. //
  31. // if(!bsuccess)
  32. // DebugLog(@"Create temp folder failed");
  33. // }
  34. if(path.length==0)
  35. path=@"";
  36. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  37. // if(appDelegate.bEnable_Cache==false)
  38. // return ;
  39. if(imgData==nil)
  40. return;
  41. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  42. NSString *cachefolder = [paths objectAtIndex:0];
  43. NSString *img_cache = [cachefolder stringByAppendingPathComponent:[NSString stringWithFormat:@"img_cache/%@",path]];
  44. NSFileManager* fileManager = [NSFileManager defaultManager];
  45. BOOL bdir=YES;
  46. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  47. {
  48. NSError *error = nil;
  49. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  50. if(!bsuccess)
  51. DebugLog(@"Create cache folder failed");
  52. // if(bsuccess)
  53. // {
  54. // sqlite3 *db = [self get_db];
  55. //
  56. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  57. // [iSalesDB close_db:db];
  58. // }
  59. }
  60. NSString *filePath = [img_cache stringByAppendingPathComponent:name];
  61. bool bsuccess=[imgData writeToFile:filePath atomically:YES];
  62. if(bsuccess)
  63. DebugLog(@"IMG CACHE SUCCESS,%@",name);
  64. else
  65. DebugLog(@"IMG CACHE FAILED,%@",name);
  66. }
  67. + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
  68. {
  69. if(path.length==0)
  70. return nil;
  71. path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  72. path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  73. // path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
  74. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  75. // if(appDelegate.bEnable_Cache==false)
  76. // return nil;
  77. NSData* data = nil;
  78. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  79. NSString *cachefolder = [paths objectAtIndex:0];
  80. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  81. NSString *filePath = [img_cache stringByAppendingPathComponent:path];
  82. NSFileManager* fileManager = [NSFileManager defaultManager];
  83. if( [fileManager fileExistsAtPath:filePath ])
  84. {
  85. data = [NSData dataWithContentsOfFile: filePath];
  86. }
  87. // NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT img FROM img_cache WHERE name = '%@'", filename];
  88. // sqlite3_stmt* statement;
  89. // sqlite3 *db = [self get_db];
  90. //
  91. //// if( sqlite3_prepare_v2(db, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
  92. //// {
  93. //// if( sqlite3_step(statement) == SQLITE_ROW )
  94. //// {
  95. //// int length = sqlite3_column_bytes(statement, 0);
  96. //// data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
  97. //// }
  98. //// }
  99. //
  100. // // Finalize and close database.
  101. // sqlite3_finalize(statement);
  102. // [iSalesDB close_db:db];
  103. return data;
  104. }
  105. @end