How can I transfer an UIImage
over WatchConnecitivity from the iPhone to the Apple Watch with no user interaction on the phone, and only loads because the watch calls for it programmatically. I need this because the image processing to create the UIImage
uses logic unavailable in the Watchkit API, so it must be created from the phone. I have seem some examples of Watch Connectivity using:
func startSession() {
session?.delegate = self
session?.activateSession()
}
However, I am new to watch kit and iOS in general and am confused on how to use this session manager, particularly to go from the watch to the device instead of the other way around like I see in examples online. Can someone please provide an example of how to do this on both the watch and the phone to call for a UIImage
on the phone from the watch?
you have to transfer your image as NSData and then decode it on watch's side. You can have a look at my blog post where I covered similar case. http://eluss.github.io/AppleWatch_iPhone_data_transfer/
This is not the way of doing it with session as I was not aware of it back then, but maybe it will help you get the whole logic.
To transfer files between your iPhone and your Apple Watch you should use Watch Connectivity, using
WCSession
to handle the communication properly. You can send anUIImage
asNSData
using thedidReceiveMessageData
delegate method of theWCSessionDelegate
.The first thing you should know is convert your
UIImage
toNSData
and viceversa. You can use for this the following code:If PNG images
If JPG images
Then you can use the
WCSession
to send the message like in the following way:In the above code when you open the
ViewController
it sends theUIImage
, the above example is only for learning purposes, you have to handle it in a more proper way regarding the complexity of your project.I hope this help you.
In InterfaceController, if you use WCSessionDelegate, remember to extend method activationDidCompleteWith: