swift ios 获取 imageView 的文件路径

发布于 2025-01-11 14:25:09 字数 736 浏览 0 评论 0原文

我已经复制了文件绝对路径并粘贴到模拟器浏览器中,图像可以打开。但 fileExists 失败,我不知道为什么......任何人都可以帮忙

 let defaultImage = "302C3FA1-E4E1-4CD8-B6DF-2FF4E4E24C11.jpeg"

loadImage(at: defaultImage)


 func fileExists(at path: String) -> Bool {
        return FileManager.default.fileExists(atPath: path)
    }

    func loadImage(at path: String) -> UIImage? {
        let tempPath = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let imagePath = "\(tempPath)\(path.trimmingCharacters(in: .whitespacesAndNewlines))"
        guard fileExists(at: imagePath) else { return nil }
        guard let image = UIImage(contentsOfFile: imagePath) else { return nil }
        return image
    }

I have already copy the file absolute path and paste in simulator browser, the image can be opened. But the fileExists is fail, i dont know why..... Can anyone help

 let defaultImage = "302C3FA1-E4E1-4CD8-B6DF-2FF4E4E24C11.jpeg"

loadImage(at: defaultImage)


 func fileExists(at path: String) -> Bool {
        return FileManager.default.fileExists(atPath: path)
    }

    func loadImage(at path: String) -> UIImage? {
        let tempPath = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let imagePath = "\(tempPath)\(path.trimmingCharacters(in: .whitespacesAndNewlines))"
        guard fileExists(at: imagePath) else { return nil }
        guard let image = UIImage(contentsOfFile: imagePath) else { return nil }
        return image
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2025-01-18 14:25:09

您需要拆分文件名和扩展文件名。

如果您使用主包。您可以遵循此代码

let stringPath = Bundle.main.path(forResource: "your_filename", ofType: "txt")

let urlPath = Bundle.main.url(forResource: "your_filename", withExtension: "txt")

,也可以使用我的代码。

func readConfigFromBundle(fileExtension: String) -> TCBConfigure? {
    let bundle = Bundle.main
    
    if let resPath = bundle.resourcePath {
        do {
            let dirContents = try FileManager.default.contentsOfDirectory(atPath: resPath)
            let filteredFiles = dirContents.filter { $0.contains(fileExtension) }
            for fileName in filteredFiles {
                let sourceURL = bundle.bundleURL.appendingPathComponent(fileName)
                let data: NSData? = NSData.init(contentsOf: sourceURL)
                if let fileData = data {
                    // implement your logic
                }
            }
        } catch {
            // implement when error
        }
    }
    return nil
}

You need split filename and extension filename.

If you use main bundle. you can follow this code

let stringPath = Bundle.main.path(forResource: "your_filename", ofType: "txt")

let urlPath = Bundle.main.url(forResource: "your_filename", withExtension: "txt")

or you can use my code.

func readConfigFromBundle(fileExtension: String) -> TCBConfigure? {
    let bundle = Bundle.main
    
    if let resPath = bundle.resourcePath {
        do {
            let dirContents = try FileManager.default.contentsOfDirectory(atPath: resPath)
            let filteredFiles = dirContents.filter { $0.contains(fileExtension) }
            for fileName in filteredFiles {
                let sourceURL = bundle.bundleURL.appendingPathComponent(fileName)
                let data: NSData? = NSData.init(contentsOf: sourceURL)
                if let fileData = data {
                    // implement your logic
                }
            }
        } catch {
            // implement when error
        }
    }
    return nil
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文