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