我只是在玩代码,我想将iOS应用程序中的图片分享到Apple Watch应用程序中。结果是,在对我说有效载荷太大之前(我降低了compressionQuality并且不再出错),然后返回“ WCErrorCodeDeliveryFailed”错误:无法传送有效载荷。
有什么建议吗?在发送图像的ViewController函数下方:
@IBAction func buttonSend(_ sender: Any) {
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
let image = UIImage(named: "test_Image.jpeg")
let data = image?.jpegData(compressionQuality: 0.5)
WCSession.default.sendMessageData(data!, replyHandler: { (data) -> Void in
// handle the response from the device
}) { (error) -> Void in
print("error: \(error.localizedDescription)")
}
}
InterfaceController端,我具有此功能,该功能应管理传输并返回回复:
func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void) {
guard let image = UIImage(data: messageData as Data) else {
return
}
DispatchQueue.main.async(execute: { () -> Void in
self.imageView.setImage(image)
})
replyHandler(messageData)
}
非常感谢!