一尘不染

如何使用Watch Connectivity传输UIImage

swift

我如何通过iPhone将UIImageOver WatchConnecitivity 从iPhone转移到Apple
Watch,而无需在手机上进行用户交互,而只能加载,因为手表以编程方式对其进行调用。我之所以需UIImage要这样做,是因为图像处理用于创建Watchkit
API中不可用的使用逻辑,因此必须通过手机进行创建。我似乎有一些使用Watch Watch连接的示例:

func startSession() {
    session?.delegate = self
    session?.activateSession()
}

但是,我是Watch
Kit的新手,对如何使用此会话管理器感到困惑,尤其是如何从手表转到设备,而不是像苹果文档中所看到的那样。有人可以提供一个示例,说明如何在手表和手机上执行此操作,以便通过手表在手机上拨打UIImage电话吗?


阅读 228

收藏
2020-07-07

共1个答案

一尘不染

要在iPhone和Apple Watch之间传输文件,您应该使用Watch
Connectivity,WCSession以正确处理通信。您可以使用的委托方法发送UIImageas
NSData``didReceiveMessageData``WCSessionDelegate

您应该知道的第一件事是将您转换UIImageNSData,反之亦然。您可以为此使用以下代码:

如果是PNG图片

let image = UIImage(named: "nameOfYourImage.jpg")
let data = UIImagePNGRepresentation(image)

如果是JPG图片

let image = UIImage(named: "nameOfYourImage.jpg")
let data = UIImageJPEGRepresentation(image, 1.0)

然后,您可以使用WCSession来通过以下方式发送消息:

ViewController.swift

class ViewController: UIViewController, WCSessionDelegate {

   override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view, typically from a nib.

       if WCSession.isSupported() {
           WCSession.defaultSession().delegate = self
           WCSession.defaultSession().activateSession()
       }

       let image = UIImage(named: "index.jpg")!

       let data = UIImageJPEGRepresentation(image, 1.0)

       WCSession.defaultSession().sendMessageData(data!, replyHandler: { (data) -> Void in
           // handle the response from the device

        }) { (error) -> Void in
              print("error: \(error.localizedDescription)")

       }
   }

   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }
}

InterfaceController.swift

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController, WCSessionDelegate {

   override func awakeWithContext(context: AnyObject?) {
       super.awakeWithContext(context)

       // Configure interface objects here.
   }

   override func willActivate() {
       // This method is called when watch view controller is about to be visible to user
       super.willActivate()

       if WCSession.isSupported() {
           WCSession.defaultSession().delegate = self
           WCSession.defaultSession().activateSession()
       } 
   }

   override func didDeactivate() {
       // This method is called when watch view controller is no longer visible
       super.didDeactivate()
   }

   func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void) {

       guard let image = UIImage(data: messageData) else {
           return
       }

       // throw to the main queue to upate properly
       dispatch_async(dispatch_get_main_queue()) { [weak self] in
           // update your UI here
       }

      replyHandler(messageData)
   }
}

在上面的代码中,当您打开时,ViewController它发送UIImage,上面的示例仅出于学习目的,您必须以更适当的方式处理项目的复杂性。

希望对您有所帮助。

2020-07-07