ios - 带有外部源的贴纸扩展

标签 ios swift ios10 imessage

我想将 iMessage 扩展添加到我现有的应用程序中以提供自定义贴纸。 首先我做了:

Xcode > File > Add Target > IMessage Extension

然后,我找到了一些可以用作测试贴纸的图片网址。

我的 Controller 看起来像这样:

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 A URL for the image displayed by this sticker. This URL must refer to a file saved on the device. The file must be a PNG, APNG, GIF, or JPEG, and must be less than 500 KB. For the best results, the image should not be smaller than 100 x 100 points or larger than 206 x 206 points. Always provide @3x images (300 x 300 pixels to 618 x 618 pixels). The system generates the @2x and @1x versions by downscaling the @3x images at runtime.

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

最佳答案

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

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

关于ios - 带有外部源的贴纸扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526523/

相关文章:

ios - PhoneGap 地理定位在 iOS10 中被阻止

ios - 我可以在 UITableViewCell 之间留出空间吗

ios - 如何在 swift 4 中使用 fontAwesome 将自定义图标添加到 UIbutton

swift - 本地通知的通知服务扩展

谷歌地图上缺少 ios mylocationbutton

swift - 创建通用单例

swift - UNTimeIntervalNotificationTrigger nextTriggerDate() 是否给出了错误的日期?

ios - 在 XCode 7.0.1 : canOpenURL: failed for URL: "fbauth2:/" 中将 FB SDK(版本 4.7.0)与 iOS 9 集成时出错

Ios uialertcontroller uiimagepicker 忽略 : app crash

javascript - jQuery Animate 在中间快速旋转和滚动文本但在结束时慢