2016-09-16 67 views
0

我想添加一個iMessage擴展到我現有的應用程序來提供自定義貼紙。 首先,我做的事:貼片擴展與外部來源

的Xcode>文件>添加目標>即時聊天擴展

然後,我發現畫面,我可以作爲測試貼紙使用的一些網址。

我的控制器看起來像這樣:

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 像素)。系統通過在運行時縮減 @ 3x圖像來生成@ 2x和@ 1x版本。

那麼你知道一種方法來保存本地圖片從服務器下載?

回答

0

只需使用擴展名(png或jpg)保存下載的圖像。 在我的例子,我保存在可丟棄緩存文件的位置(庫/緩存)

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

可以正常和動畫貼紙,方便地添加到現有的應用程序PNG文件。

添加一個新目標並選擇「貼紙應用程序擴展」。不幸的是,你必須滾動才能看到這個選項。

「iMessage Extension」是正確的選擇,如果您不僅需要貼紙,例如交互式iMessage應用程序。

New Target Dialog