Skip to content

loading images from local file system #31

@metason

Description

@metason

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions