var ljFilePath:String = NSHomeDirectory() + "/Documents/" + "LJImageCache/"
/// 写文件 /// /// - Parameters: /// - fileName: 文件名称 /// - data: 数据data /// - Returns: func writeFile(_ fileName:String,_ data:NSData) -> Bool{ //let filePath:String = NSHomeDirectory() + "/Documents/" + fileName.md5 //return data.write(toFile: filePath,atomically: true) if self.isExistsFile(path: ljFilePath) { guard let filePath : String = ljFilePath + fileName.md5 else{ return false } return data.write(toFile: filePath,atomically: true) } return false } //读取文件 -(根据路径) func readFileFromCache(_ path:String) -> NSData?{ if self.isExistsFile(path: ljFilePath) { let ljpatch = ljFilePath + path.md5 var result:NSData? do{ result = try NSData(contentsOfFile: ljpatch,options: Data.ReadingOptions.uncached) }catch{ return nil } return result } return nil } //检测文件夹是否存在,不存在则创建一个文件夹 func isExistsFile(path : String) -> Bool { let fileManager = FileManager.default let exist = fileManager.fileExists(atPath:path) if exist { //print("存在") return true }else{ //print("不存在") //不存在则创建一个文件夹 do{ try fileManager.createDirectory(at: NSURL(fileURLWithPath:path,isDirectory: true) as URL,withIntermediateDirectories: true,attributes: nil) }catch{ return false } return true } }