KMAdvertisementImage.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // [UIImagePNGRepresentation(image) writeToFile:path + name
  50. // atomically:YES]
  51. #elseif os(OSX)
  52. let cgimage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
  53. if cgimage != nil {
  54. let rep = NSBitmapImageRep.init(cgImage: cgimage!)
  55. try?rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: filePath))
  56. } else {
  57. try?image.tiffRepresentation?.write(to: URL(fileURLWithPath: filePath))
  58. }
  59. print(path)
  60. #endif
  61. }
  62. }
  63. static func fetchImageCache(url: URL) -> String {
  64. if (!FileManager.default.fileExists(atPath: kImageCacheFilePath as String)) {
  65. try?FileManager.default.createDirectory(atPath: kImageCacheFilePath as String, withIntermediateDirectories: true, attributes: nil)
  66. }
  67. var string = ""
  68. let name = NSString(string: url.path).lastPathComponent
  69. let manager = FileManager.default
  70. let contentsOfPath = try? manager.contentsOfDirectory(atPath: kImageCacheFilePath as String)
  71. if (contentsOfPath!.contains(name)) {
  72. string = (kImageCacheFilePath as String) + "/" + name
  73. // print("图片存在" + string)
  74. } else {
  75. // print("图片不存在" + (kImageCacheFilePath as String) + "/" + name)
  76. }
  77. return string
  78. }
  79. }