Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

swift - How to display image from known image path - SwiftUI

I've got an array of image URLs (all from storage, not internet). How can I show the images themselves? Simplified code:

@Binding var files:[URL]//Array of image URLs taken from an NSOpenPanel instance

Form{
    if files.count>0{
        Image(files[0].path)//Problem
    }
}
question from:https://stackoverflow.com/questions/65929936/how-to-display-image-from-known-image-path-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You say NSOpenPanel, so I'm making going to make the assumption here that we don't need to worry about waiting to load the image over a network:

if let nsImage = NSImage(contentsOf: url) {
  Image(nsImage: nsImage)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...