从Xcode项目文件夹加载图像

问题描述 投票:0回答:1

我在Xcode中有一个文件夹(untagged-data),它包含很多图像(100)。我试图将图像加载到我的应用程序,但由于某种原因路径不正确,我无法加载。这是我想要加载的URL。

file:///Users/johndoe/Library/Developer/CoreSimulator/Devices/66763907-9153-443A-BA0E-D5CB6CC9280C/data/Containers/Bundle/Application/D3C77C63-CA3D-4238-8EAD-97B1DA15F2C6/MyApp.app/untagged-data/vessey.jpg

以下是使用路径填充数组的代码:

 guard let resourceURL = Bundle.main.resourceURL else {
            fatalError("Bundle not found")
        }

        let resourcePath = resourceURL.appendingPathComponent("untagged-data")

        let paths = try! FileManager.default.contentsOfDirectory(at: resourcePath, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

        for resourceURL in paths {
            let path = resourceURL.absoluteString
            self.images.append(path)
            print(path) 

        }

当我尝试创建一个图像时,它只会抛出一个异常:

//let img = UIImage(named: images[index])
        let img = UIImage(contentsOfFile: images[index])
ios objective-c xcode
1个回答
0
投票

在safari中打开路径下面的路径以检查路径是否存在

 file:///Users/johndoe/Library/Developer/CoreSimulator/Devices/66763907-9153-443A-BA0E-D5CB6CC9280C/data/Containers/Bundle/Application/D3C77C63-CA3D-4238-8EAD-97B1DA15F2C6/MyApp.app/vessey.jpg

如果路径存在,则在代码中注释下面的行

//let resourcePath = resourceURL.appendingPathComponent("untagged-data")
© www.soinside.com 2019 - 2024. All rights reserved.