//存储图片的文件夹
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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。