2016-09-16 16 views
0

カスタムステッカーを提供するために、既存のアプリにiMessage Extensionを追加します。 まず私がやった:外部ソース付きステッカー拡張

Xcodeは>ファイル>ターゲット> IMessageが拡張

を追加し、私はテストのステッカーとして使用することができ、画像のいくつかのURLを見つけました。

そして、私のコントローラは次のようになります。

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

} 

私は私の印刷と背景色のい​​ずれかが表示されませんOKですが、私は私のステッカーを見ることができません。 アップル・ドックが言う:

fileURLこのステッカーによって表示されるイメージのURL。このURLは、デバイスに保存されているファイルを参照する である必要があります。ファイルはPNG、APNG、 GIFまたはJPEGで、500 KB未満でなければなりません。最良の結果を得るには、 の画像は、100×100ポイント以上、または206× 206ポイントを超えてはいけません。常に3倍の画像(300 x 300ピクセルから618 x 618 ピクセル)を提供してください。システムは実行時に @ 3xイメージをダウンスケーリングして@ 2xと@ 1xバージョンを生成します。

サーバーからダウンロードしたローカルピクチャを保存する方法を知っていますか?

答えて

0

ダウンロードした画像を拡張子(pngまたはjpg)で保存するだけです。 は私の例では、私はあなたが簡単に既存のアプリに通常とアニメーションステッカーを追加することができます

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

廃棄キャッシュファイルの場所にPNGファイルを保存した(ライブラリ/キャッシュ)。

新しいターゲットを追加し、「ステッカーアプリケーション拡張機能」を選択します。残念ながら、このオプションを表示するにはスクロールする必要があります。

「iMessage Extension」などのステッカーだけでなく、ステッカー以外にも「iMessage Extension」が適しています。

New Target Dialog

関連する問題