【问题标题】:Unable to display Stickers from Documents directory in iMessage extension无法在 iMessage 扩展中显示 Documents 目录中的贴纸
【发布时间】:2019-09-16 09:26:35
【问题描述】:

在我的应用中,用户可以创建图像,然后将它们用作 iMessage 中的贴纸。

我的问题是我无法显示存储在文档目录中的已创建图像。

我的问题与这个问题非常相似 - SO Question 但在我的情况下,解决方案说明没有帮助。

这是我获取图片的代码:

 func getImages(finished: () -> Void) {
    imageData.removeAll()
    let imageNames = keyboardUserDefaults!.stringArray(forKey: "Created stickers")
    for imageName in imageNames! {
        //  let imagePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
        // imageData.append(imagePath + "/" + imageName)
        imageData.append(filePath)
    }

    print("Image Data - ", imageData)

    finished()
}

这是我将它应用到 StickerView 的方法:

 func configure(usingImageName imagePath: String) {
        let urlToImage = URL(fileURLWithPath: imagePath)
        do {
            let description = NSLocalizedString("", comment: "")
            let sticker = try MSSticker(contentsOfFileURL: urlToImage, localizedDescription: description)

            print("Sicker is here - ", sticker)

            stickerView.sticker = sticker
        }
        catch {
            fatalError("Failed to create sticker: \(error)")
        }
    }

但是我得到一个没有贴纸的空白视图。

打印单元格内显示:

Sicker is here -  <MSSticker-<0x280393640> imageFileURL file:///var/mobile/Containers/Data/PluginKitPlugin/32AF9E44-F2F1-4FD1-80B5-E8E4B6C6E338/Documents/F54067B8-18DA-4737-8ED3-B716E368AF6E.png localizedDescription >

当我使用 Bundle.main.path 从 Xcode 项目内的文件夹中设置图像时,图像会显示,而不是从文档目录中设置。

【问题讨论】:

    标签: ios swift nsdocumentdirectory imessage-extension ios-stickers


    【解决方案1】:

    我找到了问题的根源:

    应用程序扩展无法访问默认文档目录文件夹。

    解决方案:

    在保存和检索图像时使用应用组并为图像创建路径:

    let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_APP_GROUP_NAME")?.appendingPathComponent("image.png")
    

    之后,您可以轻松地在应用程序扩展中访问您的图像。

    希望这对将来的人有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2017-01-04
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多