将iPhone上的UIImage传递给Apple Watch会导致手表无响应

我的手表需要从包含的应用程序请求图像。 在手表的控制器中,我有:

- (void)getOrgLogo { NSString *host = mfaInfo[@"host"]; NSDictionary *getOrgLogoRequest = @{@"request":@"getOrgLogo", @"host":host}; [MyInterfaceController openParentApplication:getOrgLogoRequest reply:^(NSDictionary *replyInfo, NSError *error) { if (error) { ... } else if (replyInfo == nil) { // I am always getting into this block!!! } else { UIImage *orgLogo = replyInfo[@"orgLogo"]; if (orgLogo != nil) { [self.orgLogoImageView setImage:orgLogo]; } } }]; } 

在我的主应用程序中,我向服务器发送请求以获取图像,然后将图像传回以观察:

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply { ... if ([[userInfo objectForKey:@"request"] isEqualToString:@"getOrgLogo"]) { NSString *host = userInfo[@"host"]; [self handleWatchKitGetOrgLogoRequest:reply withHost:host]; } ... } - (void)handleWatchKitGetOrgLogoRequest:(void (^)(NSDictionary *))reply withHost:(NSString *)host{ NSMutableDictionary *response = [[NSMutableDictionary alloc] init]; // A server request to get the image [OktaAPIClient getLogoUrlFromHost:host withSuccess:^(UIImage *image) { // I'm getting in here - indicating I successfully got the image response[@"orgLogo"] = image; // I double checked the value of response here - it's not nil! reply(response); } withFailure:^(UIImage *image) { ... reply(response); }]; } 

正如在上面的代码中所评论的那样,我在回传观察并确认它包含一个值(UIImage)之前检查了响应的值,但是在手表代码的callback中,该回复变为零。 我不知道在这个过程中会发生什么。 有什么想法吗?

另一种方法是启用应用程序组并将图像保存到共享文件夹。 如果您保存多个图像,这是非常好的。

首先启用function选项卡下的应用程序组,以用于iPhone和Apple Watch目标。

Xcode功能选项卡

然后使用iPhone上的这个代码来保存图像。

 [OktaAPIClient getLogoUrlFromHost:host withSuccess:^(UIImage *image) { NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.az.simpleSudoku"]; NSString *sharedFilePath = [groupURL.path stringByAppendingFormat:@"/%@", @"logo.png"]; [UIImagePNGRepresentation(image) writeToFile:sharedFilePath atomically:YES]; reply(response); } withFailure:^(UIImage *image) { ... reply(response); }]; 

这是为了在手表上加载图像。

 NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.az.simpleSudoku"]; NSString *sharedFilePath = [groupURL.path stringByAppendingFormat:@"/%@", @"logo.png"]; NSData *imgData = [NSData dataWithContentsOfFile:imageFilePath]; [myInterfaceImage setImageData:imgData]; 

只要你的应用程序组设置正确,这工作得很好。 我用它来在我的应用程序中来回传递数独板图像。

从我的实验看来,UIImage似乎无法被序列化并从手机上发送到手表。 所以我将UIImage转换为NSData,然后传递数据来观察,然后工作正常。

主应用程序的handleWatchKitExtensionRequesthandleWatchKitExtensionRequest

 NSMutableDictionary *response = [[NSMutableDictionary alloc] init]; [MyAPIClient getLogoUrlFromHost:host withSuccess:^(UIImage *image) { NSData *imageData = UIImagePNGRepresentation(image); [response setObject:imageData forKey:@"orgLogo"]; reply(response); } withFailure:^(UIImage *image) { NSData *imageData = UIImagePNGRepresentation(image); [response setObject:imageData forKey:@"orgLogo"]; reply(response); }]; 

手表的openParentApplication:reply:中的openParentApplication:reply:

 NSData *orgLogoData = replyInfo[@"orgLogo"]; if (orgLogoData != nil) { [self.orgLogoImageView setImage:[UIImage imageWithData:orgLogoData]]; }