问题描述
如何通过手机上没有用户互动,将 UIImage
从 WatchConnecitivity 从iPhone传输到Apple Watch,并且仅加载因为手表以编程方式调用它。我需要这个,因为创建 UIImage
的图像处理使用Watchkit API中不可用的逻辑,因此必须从手机创建。我似乎有一些使用Watch Connectivity的例子:
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()
}
但是,我是新手表套件和iOS一般,我对如何使用这个会话管理器很困惑,特别是从手表到设备,而不是像我在网上的例子中看到的那样。有人可以提供一个如何在手表和手机上执行此操作的示例,以便通过手表在手机上拨打 UIImage
吗?
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?
推荐答案
要在iPhone和Apple Watch之间传输文件,您应该使用Watch Connectivity,使用 WCSession
正确处理通信。您可以使用 didReceiveMessageData
委托发送 UIImage
作为 NSData
WCSessionDelegate
的方法。
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 an UIImage
as NSData
using the didReceiveMessageData
delegate method of the WCSessionDelegate
.
你要知道的第一件事是转换你的 UIImage
到 NSData
,反之亦然。您可以使用以下代码:
The first thing you should know is convert your UIImage
to NSData
and viceversa. You can use for this the following code:
如果是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
发送消息,如下所示:
Then you can use the WCSession
to send the message like in the following way:
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.
}
}
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
,上面的例子仅用于学习目的,你必须以更合适的方式处理它关于项目复杂性的方法。
In the above code when you open the ViewController
it sends the UIImage
, the above example is only for learning purposes, you have to handle it in a more proper way regarding the complexity of your project.
我希望这对你有帮助。
这篇关于如何使用Watch Connectivity传输UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!