【发布时间】:2017-01-24 09:21:15
【问题描述】:
我想在我现有的应用程序中添加一个 iMessage 扩展程序以提供自定义贴纸。 首先我做了:
Xcode > 文件 > 添加目标 > IMessage 扩展
然后,我找到了一些可以用作测试贴纸的图片网址。
我的控制器看起来像这样:
import UIKit
import Messages
class MessagesViewController: MSMessagesAppViewController, MSStickerBrowserViewDataSource {
var stickers = [MSSticker]();
var url = ["http://iconizer.net/files/Brightmix/orig/monotone_close_exit_delete_small.png","https://upload.wikimedia.org/wikipedia/commons/d/d5/Japan_small_icon.png"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(" ----- HERE");
loadStickers();
createStickerBrowser();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Stickers Handling
func loadStickers() {
print(" ----- loadStickers");
for i in 0...1 {
do {
let sticker = try MSSticker(contentsOfFileURL: URL(string: url[i])!, localizedDescription: "\(i)")
print(" \(i) : \(sticker)");
stickers.append(sticker)
} catch {
print("error \(error)");
}
}
}
func createStickerBrowser() {
print(" ----- createStickerBrowser");
let controller = MSStickerBrowserViewController(stickerSize: .large)
addChildViewController(controller)
view.addSubview(controller.view)
controller.stickerBrowserView.backgroundColor = UIColor.gray
controller.stickerBrowserView.dataSource = self;
view.topAnchor.constraint(equalTo: controller.view.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor).isActive = true
view.leftAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: controller.view.rightAnchor).isActive = true
}
// MARK: - MSStickerBrowserViewDataSource
func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
return stickers.count
}
func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
return stickers[index]
}
// MARK: - Conversation Handling
override func willBecomeActive(with conversation: MSConversation) {
// Called when the extension is about to move from the inactive to active state.
// This will happen when the extension is about to present UI.
// Use this method to configure the extension and restore previously stored state.
print("----- willBecomeActive");
}
override func didResignActive(with conversation: MSConversation) {
// Called when the extension is about to move from the active to inactive state.
// This will happen when the user dissmises the extension, changes to a different
// conversation or quits Messages.
// Use this method to release shared resources, save user data, invalidate timers,
// and store enough state information to restore your extension to its current state
// in case it is terminated later.
print("----- didResignActive");
}
override func didReceive(_ message: MSMessage, conversation: MSConversation) {
// Called when a message arrives that was generated by another instance of this
// extension on a remote device.
// Use this method to trigger UI updates in response to the message.
}
override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
// Called when the user taps the send button.
}
override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
// Called when the user deletes the message without sending it.
// Use this to clean up state related to the deleted message.
}
override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
// Called before the extension transitions to a new presentation style.
// Use this method to prepare for the change in presentation style.
}
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
// Called after the extension transitions to a new presentation style.
// Use this method to finalize any behaviors associated with the change in presentation style.
}
}
我没有看到我的任何印刷品,背景颜色还可以,但我看不到我的贴纸。 苹果文档这样说:
fileURL 此标签显示的图像的 URL。此网址必须 参考保存在设备上的文件。文件必须是 PNG、APNG、 GIF 或 JPEG,并且必须小于 500 KB。为了获得最佳结果, 图像不应小于 100 x 100 点或大于 206 x 206 分。始终提供@3x 图像(300 x 300 像素到 618 x 618 像素)。系统通过降尺度生成@2x 和@1x 版本 运行时的@3x 图像。
那么你知道一种保存从服务器下载的图片到本地的方法吗?
【问题讨论】: