将 UIImage 从 iPhone 传递到 Apple Watch 导致手表中的响应为零 [英] Passing UIImage from iPhone to Apple Watch results in nil response in watch

查看:16
本文介绍了将 UIImage 从 iPhone 传递到 Apple Watch 导致手表中的响应为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My watch needs to request an image from the containing app. In the watch's controller, I have:

- (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];
            }
        }
    }];
}

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

In my main app, I send a request to the server to get the image, then pass the image back to watch:

- (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),但是在手表代码的回调中,此回复变为 nil.我不知道在这个过程中会发生什么.有什么想法吗?

As commented in the code above, I checked the value of response right before I pass it back to watch and confirmed that it contain a value (a UIImage), however in the callback of the watch code, this reply becomes nil. I do not know what happens in the process. Any thoughts?

推荐答案

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

Another way to do this is to enable app groups and save the images to a shared folder. This is really good if you are saving multiple images.

首先在功能选项卡下为您的 iphone 和 Apple Watch 目标启用应用组.

Start by enabling app groups under the capabilities tab for both your iphone and apple watch target.

然后在 iPhone 上使用此代码保存图像.

Then use this code on the iPhone to save the image.

[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);
}];

这是在手表上加载该图像.

And this to load that image on the watch.

NSURL *groupURL = [[NSFileManager defaultManager]
  containerURLForSecurityApplicationGroupIdentifier:@"group.az.simpleSudoku"];

NSString *sharedFilePath = [groupURL.path 
  stringByAppendingFormat:@"/%@", @"logo.png"];

NSData *imgData = [NSData dataWithContentsOfFile:imageFilePath];

[myInterfaceImage setImageData:imgData];

只要您的应用组设置正确,这将非常有效.我用它在我的应用中来回传递数独图板图像.

As long as your app groups are set up correctly, this works very well. I use it to pass sudoku board images back and forth in my app.

这篇关于将 UIImage 从 iPhone 传递到 Apple Watch 导致手表中的响应为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆