KMAdvertisementImage.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // KMAdvertisementImage.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2023/1/12.
  6. // 图片缓存
  7. import Foundation
  8. import AppKit
  9. typealias KMAdvertisementImageCompletion = (_ image: NSImage) -> Void
  10. @objc class KMAdvertisementImage: NSObject {
  11. @objc static func imageWithURL(url : URL?, completion: KMAdvertisementImageCompletion?) -> NSImage {
  12. var image: NSImage = NSImage()
  13. if url != nil {
  14. //获取缓存图片是否存在
  15. let imageString: String = KMAdvertisementImageCache.fetchImageCache(url: url!)
  16. if imageString != "" {
  17. //存在获取本地图片
  18. image = NSImage.init(contentsOfFile: imageString) ?? NSImage()
  19. if completion != nil {
  20. completion!(image)
  21. }
  22. } else {
  23. let path = KMAdvertisementImageCache.kImageCacheFilePath
  24. let filePath = (path as String) + "/" + (NSString(string: NSString(string: url!.path).lastPathComponent) as String)
  25. KMAdvertisementRequestServer.requestServer.downLoad(inputURL: url!, filePath: filePath) { progress in
  26. } completion: { task, responseObject, error in
  27. if completion != nil {
  28. DispatchQueue.main.async {
  29. image = NSImage.init(contentsOfFile: KMAdvertisementImageCache.fetchImageCache(url: url!)) ?? NSImage()
  30. completion!(image)
  31. }
  32. }
  33. }
  34. }
  35. }
  36. return image
  37. }
  38. }
  39. class KMAdvertisementImageCache: NSObject {
  40. static let kImageCacheFilePath: NSString = NSTemporaryDirectory() + "Advertisement/ImageCache" as NSString
  41. static func saveImageCache(image: NSImage, name: String) {
  42. if image.size.width != 0 {
  43. let path = KMAdvertisementImageCache.kImageCacheFilePath
  44. let filePath = (path as String) + "/" + NSString(string: name).deletingPathExtension + ".png"
  45. if (!FileManager.default.fileExists(atPath: path as String)) {
  46. try?FileManager.default.createDirectory(atPath: path as String, withIntermediateDirectories: true, attributes: nil)
  47. }
  48. #if os(iOS)
  49. #elseif os(OSX)
  50. let cgimage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
  51. if cgimage != nil {
  52. let rep = NSBitmapImageRep.init(cgImage: cgimage!)
  53. try?rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: filePath))
  54. } else {
  55. try?image.tiffRepresentation?.write(to: URL(fileURLWithPath: filePath))
  56. }
  57. print(path)
  58. #endif
  59. }
  60. }
  61. static func fetchImageCache(url: URL) -> String {
  62. if (!FileManager.default.fileExists(atPath: kImageCacheFilePath as String)) {
  63. try?FileManager.default.createDirectory(atPath: kImageCacheFilePath as String, withIntermediateDirectories: true, attributes: nil)
  64. }
  65. var string = ""
  66. let name = NSString(string: url.path).lastPathComponent
  67. let manager = FileManager.default
  68. let contentsOfPath = try? manager.contentsOfDirectory(atPath: kImageCacheFilePath as String)
  69. if (contentsOfPath!.contains(name)) {
  70. string = (kImageCacheFilePath as String) + "/" + name
  71. }
  72. return string
  73. }
  74. }