带外部来源的贴纸扩展程序

时间:2016-09-16 08:02:55

标签: ios swift ios10 imessage

我想在现有应用中添加iMessage Extension以提供自定义贴纸。 首先我做了:

  

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

}

我没有看到我的任何印刷品,背景颜色没问题,但我看不到我的贴纸。 Apple doc说:

  

fileURL此贴纸显示的图像的URL。此URL必须   指的是保存在设备上的文件。该文件必须是PNG,APNG,   GIF或JPEG,且必须小于500 KB。为了获得最佳效果,   图像不应小于100 x 100点或大于206 x   206点。始终提供@ 3x图像(300 x 300像素到618 x 618   像素)。系统通过缩减生成@ 2x和@ 1x版本   运行时的@ 3x图像。

那么你知道如何保存从服务器下载的本地图片吗?

2 个答案:

答案 0 :(得分:0)

使用扩展程序(png或jpg)保存下载的图像。 在我的例子中,我将png文件保存在可丢弃缓存文件(库/缓存)的位置

[...]/Library/Caches/StickerCache/Pack_1188767/image_166176391.png

答案 1 :(得分:0)

您可以轻松地将普通和动画贴纸添加到现有应用中。

添加新目标并选择" Sticker App Extension"。不幸的是,您必须滚动才能看到此选项。

" iMessage Extension"如果您想要的不仅仅是贴纸,例如Interactive iMessage Apps。

,这是正确的选择

New Target Dialog