// // FileCache.m // RedAnt Mobile // // Created by Ray on 08/09/2017. // Copyright © 2017 Ray. All rights reserved. // #import "FileCache.h" #import "AppDelegate.h" #import "const.h" @implementation FileCache + (void) cache_img: (NSData*) imgData filename:(NSString*) name saveTo:(NSString*) path { path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""]; path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""]; path=[path stringByReplacingOccurrencesOfString:name withString:@""]; // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); // NSString *cachefolder = [paths objectAtIndex:0]; // NSString *img_cache = [cachefolder stringByAppendingPathComponent:[NSString stringWithFormat:@"img_cache/%@",@"www.newpacificdirect.com/u/NPD/20160615/mytest/"]]; // if ([[NSFileManager defaultManager] fileExistsAtPath:img_cache]) { // // DebugLog(@"目录已经存在了"); // // } // else // { // NSError *error = nil; // bool bsuccess=[[NSFileManager defaultManager] createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error]; // // if(!bsuccess) // DebugLog(@"Create temp folder failed"); // } if(path.length==0) path=@""; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // if(appDelegate.bEnable_Cache==false) // return ; if(imgData==nil) return; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachefolder = [paths objectAtIndex:0]; NSString *img_cache = [cachefolder stringByAppendingPathComponent:[NSString stringWithFormat:@"img_cache/%@",path]]; NSFileManager* fileManager = [NSFileManager defaultManager]; BOOL bdir=YES; if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir]) { NSError *error = nil; bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error]; if(!bsuccess) DebugLog(@"Create cache folder failed"); // if(bsuccess) // { // sqlite3 *db = [self get_db]; // // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db]; // [iSalesDB close_db:db]; // } } NSString *filePath = [img_cache stringByAppendingPathComponent:name]; bool bsuccess=[imgData writeToFile:filePath atomically:YES]; if(bsuccess) DebugLog(@"IMG CACHE SUCCESS,%@",name); else DebugLog(@"IMG CACHE FAILED,%@",name); } + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path { if(path.length==0) return nil; path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""]; path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""]; // path=[path stringByReplacingOccurrencesOfString:filename withString:@""]; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // if(appDelegate.bEnable_Cache==false) // return nil; NSData* data = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachefolder = [paths objectAtIndex:0]; NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"]; NSString *filePath = [img_cache stringByAppendingPathComponent:path]; NSFileManager* fileManager = [NSFileManager defaultManager]; if( [fileManager fileExistsAtPath:filePath ]) { data = [NSData dataWithContentsOfFile: filePath]; } // NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT img FROM img_cache WHERE name = '%@'", filename]; // sqlite3_stmt* statement; // sqlite3 *db = [self get_db]; // //// if( sqlite3_prepare_v2(db, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK ) //// { //// if( sqlite3_step(statement) == SQLITE_ROW ) //// { //// int length = sqlite3_column_bytes(statement, 0); //// data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length]; //// } //// } // // // Finalize and close database. // sqlite3_finalize(statement); // [iSalesDB close_db:db]; return data; } @end