NetworkImage icon indicating copy to clipboard operation
NetworkImage copied to clipboard

loading images from local file system

Open metason opened this issue 2 years ago • 0 comments

Added the if url.isFileURL {... } code so that local images (also with relative path and baseURL = file:://) do work. Feel free to add it to the main branch.

extension DefaultNetworkImageLoader: NetworkImageLoader {
  public func image(from url: URL) async throws -> CGImage {
    if let image = self.cache.image(for: url) {
      return image
    }
      if url.isFileURL {
          let data = try Data(contentsOf: url)
          guard let source = CGImageSourceCreateWithData(data as CFData, nil),
            let image = CGImageSourceCreateImageAtIndex(
                source, 0, [kCGImageSourceShouldCache: true] as CFDictionary
            ) else {
              throw URLError(.cannotDecodeContentData)
          }
          // add image to cache
          self.cache.setImage(image, for: url)
          return image
      }
    ...

Thanks for NetworkImage and swift-markdown-ui. I use them in a macOS project for a low-code IDE to support documentation.

metason avatar Oct 06 '23 17:10 metason