无法读取应用组 ios 中的共享容器

Not able to read from shared containor in app group ios

我创建了一个应用程序组 group.com.example.FoodTracker-qg,在应用程序的主视图控制器中,我正在下载图像并将其存储在共享容器中,但我无法在图像视图中使用相同的图像.我收到以下错误

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=256 "The file “EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg” couldn’t be opened." UserInfo={NSURL=/private/var/mobile/Containers/Shared/AppGroup/EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType.swift, line 178

下面是我从共享容器写入和读取的代码

//  ViewController.swift


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var mealName: UILabel!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "https://static.pexels.com/photos/3247/nature-forest-industry-rails.jpg"
        try? Data(contentsOf: URL(string: urlString)!).write(to: getSharedFileUrl("image.jpg"))
        imageView.image = UIImage(data: try! Data(contentsOf: getSharedFileUrl("image.jpg")))                  
    }

    func getSharedFileUrl(_ fileName: String) -> URL {
        let fileManager = FileManager.default
        let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.FoodTracker-qg")
        return URL(string: url!.path.appending(fileName))!
    } 
}

代码似乎是正确的,您的问题可能是由于文件名本身.... 检查url错误

/private/var/mobile/Containers/Shared/AppGroup/EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg

没有“/”……应该是 728/image.jpg

getSharedFileUrl(_ fileName: String) -> URL 为什么要从 containerURL(.. 解构新检索的 url 只是为了创建一个新的?

您可以改为这样做:

return url!.appending(fileName)

这应该可以解决您缺少 / 的问题。

我不确定我是否同意那些强制换行,或者甚至可能用 Data(contentsOf: ..) 阻塞主线程!

append文件名。因此,您在路径组件之间缺少斜杠 /,因为 JJAAXX44 已正确指出。

改用appendingPathComponent

func getSharedFileUrl(_ fileName: String) -> URL {
    let fileManager = FileManager.default
    guard let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.FoodTracker-qg")
        else {
            fatalError() // handle error appropriate to your needs instead
    }

    return url.appendingPathComponent(fileName)
}

你的写操作不是correct.So你无法从你的共享容器中读取你的图像。

让 appGroupIdentifier = "group.mark3"

extension Data {

    func writeToGroupContainer(filePath: String, completeHandle: @escaping (Bool, String) -> Void) {

        let url = Data.appGroupContainerURL(filePath: filePath)

        let result = FileManager.default.createFile(atPath: url.path, contents: self, attributes: nil)
        completeHandle(result, url.path)
    }

    static func appGroupContainerURL(filePath: String) -> URL {

        let fileManager = FileManager.default
        let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
        let url = groupURL!.appendingPathComponent(filePath)
        try? FileManager.default.createDirectory(atPath: url.deletingLastPathComponent().path, withIntermediateDirectories: true, attributes: nil)
        return url
    }

}

使用此扩展程序将数据正确写入您的磁盘。

override func viewDidLoad() {
    super.viewDidLoad()

    let filePath = "images/group/mark3/avatar.png"
    let data: Data = UIImagePNGRepresentation(UIImage(named: "image.png")!)!
    data.writeToGroupContainer(filePath: filePath) { (done, file) in
        print(filePath, done)
    }

}