gpt4 book ai didi

swift - 如何从 macOS Swift 上的文档目录加载图像?

转载 作者:行者123 更新时间:2023-12-04 08:41:15 24 4
gpt4 key购买 nike

我正在努力将图像加载到 Image。我正在制作一个与 iOS 略有不同的 MacOS 应用。

我搜索了互联网,但我只找到了几个如何在 iOS 中制作它的答案。

请检查代码:

import SwiftUI
struct ContentView: View {
let myUrl = URL(fileURLWithPath: "/Users/codegrinder/Documents/turtlerock.jpg")
init() {
//TODO Do something with the myUrl which is valid (not nil)
}
var body: some View {
Image("turtlerock")
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment:.center)
//I need to put the param to Image what I want to load an image from myUrl

}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

最佳答案

首先点击你的app target > signing & capabilities 然后移除App Sandbox,如下图所示:

enter image description here

现在您可以访问不在您的应用程序包内的文件。

struct ContentView: View {
let image = NSImage(contentsOf: URL(fileURLWithPath: "/Users/codegrinder/Documents/turtlerock.jpg"))!
var body: some View {
Image(nsImage: image)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment:.center)
}
}

关于swift - 如何从 macOS Swift 上的文档目录加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64561842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com