【发布时间】: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