我正在尝试制作一个贴纸包,该贴纸包在设备上本地没有贴纸图像,但可以从网站上获取它们。
我已经在iMessage应用程序中尝试过此操作,但是UI中没有任何显示。要进一步扩展我想做的事情:我希望项目中没有图像,但希望能够从网站上下载图像以使用标签。这是我的代码:
import Foundation
import Messages
class CustomStickers : MSStickerBrowserViewController {
var Stickers : [MSSticker]!
var stickerNames = ["image8-2"]
override func viewDidLoad() {
super.viewDidLoad()
self.Stickers = [MSSticker]()
self.Stickers = loadStickers()
let urlDownload = URL(string: "https://blog.hubspot.com/hubfs/image8-2.jpg")!
downloadImage(from: urlDownload)
}
func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}
func downloadImage(from url: URL) {
print("Download Started")
getData(from: url) { data, response, error in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
}
}
override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
return self.Stickers.count
}
override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
return self.Stickers[index]
}
private func loadStickers() -> [MSSticker] {
var Stickers = [MSSticker]()
for stickerName in self.stickerNames {
guard let url = Bundle.main.url(forResource: stickerName, withExtension: "jpg") else {
fatalError("Image does not exist")
}
let sticker = try! MSSticker(contentsOfFileURL: url, localizedDescription: stickerName)
Stickers.append(sticker)
}
return Stickers
}
}
同样,该应用程序应显示google徽标贴纸,但不显示任何内容。是的,我已经将此代码连接到了VC。