来自路径的SwiftUI UIImage不可见

来自路径的SwiftUI UIImage不可见,swift,swiftui,Swift,Swiftui,我在应用程序组中存储了一个图像,但我无法显示该图像,我不知道为什么 我的观点是: Image(uiImage: getImageFromDir(imageName: name)!) .resizable() 我使用以下函数获取图像: func getImageFromDir(imageName: String) -> UIImage? { let appGroupPath = FileManager.default.containerURL(forSecurityAppl

我在应用程序组中存储了一个图像,但我无法显示该图像,我不知道为什么

我的观点是:

Image(uiImage: getImageFromDir(imageName: name)!)
   .resizable()
我使用以下函数获取图像:

func getImageFromDir(imageName: String) -> UIImage? {
  
  let appGroupPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myId")!
  
  let imagePath = appGroupPath.appendingPathComponent(imageName)
  do {
    let imageData = try Data(contentsOf: imagePath)
    return UIImage(data: imageData)
  } catch {
    print("Error loading image : \(error)")
  }
  return nil
}
这运行正常,捕获块从未命中,但图像仍然不可见。我最初的想法是我有一个无效的路径,但情况似乎并非如此,因为我可以使用该路径在React Native中按预期加载图像


“我的样式”也没有问题,因为从Assets.xcsets加载的不同图像工作正常。

假设文件确实存在于指定位置(您可以验证生成的URL),请尝试使用安全范围的资源包装器,如下所示

func getImageFromDir(imageName: String) -> UIImage? {
  
  let appGroupPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myId")!
  
  let imagePath = appGroupPath.appendingPathComponent(imageName)
  do {
    if imagePath.startAccessingSecurityScopedResource() {  // << this !!
        defer {
            imagePath.stopAccessingSecurityScopedResource()   // << and this !!
        }
         let imageData = try Data(contentsOf: imagePath)
         return UIImage(data: imageData)
        }
  } catch {
    print("Error loading image : \(error)")
  }
  return nil
}
func getImageFromDir(imageName:String)->UIImage?{
让appGroupPath=FileManager.default.containerURL(用于安全应用程序组标识符:“group.myId”)!
让imagePath=appGroupPath.appendingPathComponent(imageName)
做{

如果imagePath.startAccessingSecurityScopedResource(){/在我的解决方案工作时,它不是我的图像为什么不显示的有效答案,我仍然想知道为什么,如果有人知道将来会看到这篇文章的话


为了解决这个问题,我没有使用图像的绝对路径,而是使用base64字符串作为数据。图像现在成功地显示出来。

不幸的是,imagePath.startAccessingSecurityScopedResource()返回false。我还可以绝对确认文件的路径正确。当查看
imageData
的数据并单击导出->另存为
myimage.jpg
时,我可以在finder中成功打开图像。