How to transfer UIImage using Watch Connectivity

How can I transfer UIImage more WatchConnecitive from iPhone to Apple Watch without user intervention on the phone and download only because the watch requires it programmatically. I need this because image processing to create UIImage uses logic that is not available in the Watchkit API, so it needs to be created from the phone. I have some examples of Watch Connectivity using:

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

However, I am new to watching the kit and iOS in general, and am confused about how to use this session manager, in particular, to switch from the watch to the device, and not vice versa, as I see in the examples on the Internet. Can someone provide an example of how to do this on both the watch and the phone to call UIImage on the phone from the watch?

+8
ios swift swift2 watchkit watch-os-2
source share
3 answers

To transfer files between your iPhone and your Apple Watch, you must use Watch Connectivity using WCSession to properly process the message. You can send UIImage as NSData using the didReceiveMessageData delegation didReceiveMessageData for WCSessionDelegate .

The first thing you need to know is to convert UIImage to NSData and vice versa. You can use the following code for this:

If PNG images

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

If jpg images

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

Then you can use WCSession to send the message as follows:

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

In the above code, when you open the ViewController it sends a UIImage , the above example is for educational purposes only, you should handle it in a more proper way in relation to the complexity of your project.

Hope this helps you.

+10
source share

you need to transfer your image as NSData and then decode it on the view side. You can take a look at my blog post where I looked at a similar case. http://eluss.imtqy.com/AppleWatch_iPhone_data_transfer/

This is not a way to do this with a session, since I did not know about it then, but perhaps it will help you get all the logic.

0
source share

In InterfaceController, if you are using WCSessionDelegate, remember to extend the activation method of DidCompleteWith:

 func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } 
0
source share

All Articles