请尝试使用以下代码
Save Data let placeholderURL: URL = getDocumentDirectoryPath()!.appendingPathComponent("\(imageName.JPG)/", isDirectory: true) let fileExists: Bool = FileManager.default.fileExists(atPath: placeholderURL.path ) if !fileExists { let thumbnailImageURL = URL(string: firebaseURL) var placeholderData: Data? = nil if let url = url { do { placeholderData = try Data(contentsOf: (thumbnailImageURL ?? nil)!) } catch { print("Unable to load data: \(error)") } } if urlData != nil { do { //saving is done on main thread try placeholderData?.write(to: URL(fileURLWithPath: placeholderURL.path) , options: .atomic) } catch { print(error) } } }
回到图像
let thumbnailURL: URL = getDocumentDirectoryPath()!.appendingPathComponent("\("imageName.JPG")/", isDirectory: true) let fileExists: Bool = FileManager.default.fileExists(atPath: thumbnailURL.path) var urlThumbnail:URL? = nil if fileExists { urlThumbnail = URL(fileURLWithPath: thumbnailURL.path) } else { urlThumbnail = URL(string: firebaseURL) }
图像视图中的静止图像
self.imgtemp.sd_setImage(with: urlThumbnail, placeholderImage: UIImage(named: "placeholder.png"))