将图像从 iPhone 传递到 Apple Watch 无效
Passing an image from iPhone to Apple Watch not working
大多数关于传递图像的教程都讨论现有图像。我试图在运行时在 iPhone 上生成图像并传递给 Apple Watch。
我使用的是 MMWormhole
中引用的 但这并没有让我的图像通过。
我现在尝试在 WKInterfaceController
上直接使用 + openParentApplication
。我得到一个错误:
Property list invalid for format: 200 (property lists cannot contain NULL)
我查了一下,发现 UIImage 无法序列化。所以现在我尝试转换 UIImage > NSData > NSString
并发送
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(loop) userInfo:nil repeats:true];
};
- (void)loop {
[WKInterfaceController openParentApplication:nil reply:^(NSDictionary *replyInfo, NSError *error) {
NSData *m = replyInfo[@"image"];
UIImage *img = [UIImage imageWithData:m];
[self.image setImage:img];
}];
// [self.wormhole passMessageObject:@"hello" identifier:@"hello"];
}
AppDelegate
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
UIImage *image = [UIImage imageNamed:@"cat.png"];
NSData *data = UIImagePNGRepresentation(image);
NSDictionary *d = @{@"image" : data};
reply(d);
}
您需要在两端指定app group id。喜欢:
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:appGroupId optionalDirectory:kWormholeDirectory];
大多数关于传递图像的教程都讨论现有图像。我试图在运行时在 iPhone 上生成图像并传递给 Apple Watch。
我使用的是 MMWormhole
中引用的
我现在尝试在 WKInterfaceController
上直接使用 + openParentApplication
。我得到一个错误:
Property list invalid for format: 200 (property lists cannot contain NULL)
我查了一下,发现 UIImage 无法序列化。所以现在我尝试转换 UIImage > NSData > NSString
并发送
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(loop) userInfo:nil repeats:true];
};
- (void)loop {
[WKInterfaceController openParentApplication:nil reply:^(NSDictionary *replyInfo, NSError *error) {
NSData *m = replyInfo[@"image"];
UIImage *img = [UIImage imageWithData:m];
[self.image setImage:img];
}];
// [self.wormhole passMessageObject:@"hello" identifier:@"hello"];
}
AppDelegate
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
UIImage *image = [UIImage imageNamed:@"cat.png"];
NSData *data = UIImagePNGRepresentation(image);
NSDictionary *d = @{@"image" : data};
reply(d);
}
您需要在两端指定app group id。喜欢:
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:appGroupId optionalDirectory:kWormholeDirectory];