简体   繁体   English

无法从应用程序组iOS中的共享容器读取

[英]Not able to read from shared containor in app group ios

I have created an app group group.com.example.FoodTracker-qg, In the main view controller of the app I am downloading an image and storing inside the shared container but I am unable to use the same image in image view. 我创建了一个应用程序组group.com.example.FoodTracker-qg,在该应用程序的主视图控制器中,我正在下载图像并将其存储在共享容器中,但无法在图像视图中使用同一图像。 I am getting the following error 我收到以下错误

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." 表达式意外引发错误:Error Domain = NSCocoaErrorDomain Code = 256“无法打开文件“ EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg”。 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 UserInfo = {NSURL = / private / var / mobile / Containers / Shared / AppGroup / EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg}:文件/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0 .63 / src / swift / stdlib / public / core / ErrorType.swift,第178行

Below is my code for writing and reading from shared container 以下是我用于从共享容器写入和读取的代码

//  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))!
    } 
}

Code seems to be correct, your problem might be due to file name itself.... check url in error 代码似乎正确,您的问题可能是由于文件名本身引起的。...检查url错误

/private/var/mobile/Containers/Shared/AppGroup/EC700C58-E9C9-480D-8EE2-B88A570A5 728image .jpg / private / var / mobile /容器/共享/ AppGroup / EC700C58-E9C9-480D-8EE2-B88A570A5 728图像 .jpg

there's no "/"...it should look like 728/image.jpg 没有“ /” ...应该看起来像是728 / image.jpg

In getSharedFileUrl(_ fileName: String) -> URL why deconstruct the newly retrieved url from containerURL(.. just to create a new one? getSharedFileUrl(_ fileName: String) -> URL为什么要从containerURL(.. getSharedFileUrl(_ fileName: String) -> URL解构新检索的url只是为了创建一个新的url

You could do something like this instead: 您可以改为执行以下操作:

return url!.appending(fileName)

This should fix your problem with the missing / . 这应该可以解决缺少/问题。

I'm not sure I agree with those force wraps, or even with possibly blocking the Main Thread with Data(contentsOf: ..) though! 我不确定我是否同意这些强制换行,甚至不确定是否可能使用Data(contentsOf: ..)阻塞主线程!

You append the filename. append文件名。 As a result, you are missing the slash / in between the path components, as JJAAXX44 correctly pinpointed. 结果,由于正确确定了JJAAXX44,因此您在路径组件之间缺少了斜杠/

Use appendingPathComponent instead: 请改用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)
}

Your write action is not correct.So you can not be read you image form your shared container. 您的写入操作不正确。因此无法从共享容器中读取图像。

let appGroupIdentifier = "group.mark3" 让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
    }

}

us this extension to write a data to your disk correct. 我们这个扩展名正确地将数据写入磁盘。

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)
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM