I have an iPhone app that captures a UIView as UIImage after 5-10 seconds. Is it possible to pass that UIImage to my Apple Watch app and show it in UIImageView there?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes it is. All you need to do is create a NSDictionary
like:
var dict:[NSString : UIImage] = ["image" : yourImage]
And then you just use the following methods to communicate between the Apple Watch and the iOS app: LINK
回答2:
The answers here are only half correct cause you can't send an UIImage directly! You need to send your images as NSData like f.e. so:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply
{
reply(@{@"image" : UIImagePNGRepresentation(yourUIImage)});
// JPEG Representation would of course also work
}
回答3:
Yes you can do.
You can pass data using this method
- (IBAction)passUserInfo:(id)sender
{
NSDictionary *userInfo = @{@"image":[UIImage imageNamed:@"test"]};
[WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error){
if ([replyInfo objectForKey:@"success"]) {
NSLog(@"Sent your data");
}
}];// userinfo must be non-nil
}
and you can handle your data in this method.
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
UIImage *image = [userInfo objectForKey:@"image"];
NSDictionary *replyInfo = @{@"success":@"success",@"error":@""};
reply(replyInfo);
}
Otherwise see this Tutorial
MMWormhole