swift-markdown-ui icon indicating copy to clipboard operation
swift-markdown-ui copied to clipboard

Image not shown

Open OLFDB opened this issue 10 months ago • 2 comments

Describe the bug I'm trying to show an image in MarkdownUI by using

Markdown("Hallo ![myimage](Image.png)", baseURL: docdir, imageBaseURL: docdir)

The image is inside the directory docdir. There is no error shown in the console but only the text "Hallo" shows up. If I use

Markdown("Hallo ![myimage](Image)", baseURL: docdir, imageBaseURL: docdir)

I get this error in the console:

    "LocalDataTask <38A8317F-D044-4C43-B2DB-B6BD271E35EB>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <38A8317F-D044-4C43-B2DB-B6BD271E35EB>.<1>, NSUnderlyingError=0x300e94780 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "(null)"}}

It seems the directory used for searching the image is correct and the image is found when Image.png is used as the filename.

Checklist

  • [ x] I can reproduce this issue with a vanilla SwiftUI project.
  • [ x] This bug hasn't been addressed in an existing GitHub issue.

Steps to reproduce Create a Swift-UI project in Xcode and use this as ContentView.swift

import SwiftUI
import MarkdownUI

let docdir = getDocumentDir()
let copyTask = saveImageToDocuments()

struct ContentView: View {
    var body: some View {
        VStack {
           if(docdir.isFileURL) {
                if(!copyTask.isEmpty) {
                    Markdown("Hallo ![myimage](Image.png)", baseURL: docdir, imageBaseURL: docdir)
                }
            }
        }
        .padding()
    }
}

func getDocumentDir()->URL {
    let fileManager = FileManager.default
    guard let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
        print("Dokumentenordner nicht gefunden")
        return URL(fileURLWithPath: "Dokumentenordner nicht gefunden")
    }
    return documentDirectory
}

func saveImageToDocuments()->String {
    guard let image = UIImage(named: "image") else {
        print("Bild nicht gefunden")
        return "Bild nicht gefunden"
    }
    
    guard let imageData = image.pngData() else {
        print("Fehler beim Umwandeln des Bildes in Data")
        return "Fehler beim Umwandeln des Bildes in Data"
    }
    
    let fileManager = FileManager.default
    guard let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
        print("Dokumentenordner nicht gefunden")
        return "Dokumentenordner nicht gefunden"
    }
    
    // Dateipfad festlegen (hier PNG-Dateiformat)
    let filePath = documentDirectory.appendingPathComponent("Image.png")
    
    do {
        try imageData.write(to: filePath)
        print("Bild erfolgreich gespeichert unter: \(filePath)")
    } catch {
        print("Fehler beim Speichern des Bildes: \(error)")
        return "Fehler beim Speichern des Bildes: \(error)"
    }
    return "Bild erfolgreich gespeichert unter: \(filePath)"
}

#Preview {
    ContentView()
}

Add MarkdownUI package Add an image "image" to the asset catalog.

Expected behavior The image is shown inside the view.

Version information

  • MarkdownUI: 2.4.1
  • OS: iOS 18.3.1
  • Xcode: 16.2

OLFDB avatar Mar 01 '25 12:03 OLFDB

FYI inline images inside markdown encoded in base64 also does not seem to work. For example following markdown does not display the image:

Markdown **with**
test *image* ![Inline image](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAgVBMVEX///8BVo0BWZH6/P3a5e0AVIwAN4L2+/3t8vXU3ua0xtY1bZshW48KV44ATooARorz+v36+/zu9fne6fDE1+O3ytmtxdeivdGqu86ktciTq8J9pMB6nrthiatHgqpNe6Myc6AwX5EhXpEkXJAbWI4vWY0eVYsAUIoAS4gAL4MALH9pQsNDAAAAmElEQVQY01WPRxLDIBAEAQEyWVbOOf//gRZl5LL6MjV9mK0FDs8DT9L02f2u8//qGyRaJ1f8CJp9bwK3FkaMFUZKUzAWhR7AbY8OOkM40wP1LQZeTpdBwgs5LDS317khygpFDP/OcESsIIgDh9igUnATd3+NZNJ6IuPLiRitVAi6otiJ7EQlxiU6MyeqGtvAdXVv+O5Du/EBIX0IN9TQfSgAAAAASUVORK5CYII=) inside.

leszek-s avatar Mar 04 '25 16:03 leszek-s

I also encountered such a situation

Yyilin001 avatar Apr 20 '25 01:04 Yyilin001