Skip to content

Image not shown #393

@OLFDB

Description

@OLFDB

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

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