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

查看:93
本文介绍了从iPhone传递UIImage到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.

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

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