如何将 UIImage 从 iPhone 应用程序传递到 Apple Watch 应用程序
How to pass UIImage from iPhone app to Apple Watch app
我有一个 iPhone 应用程序可以在 5-10 秒后将 UIView 捕获为 UIImage。是否可以将该 UIImage 传递到我的 Apple Watch 应用程序并在 UIImageView 中显示它?
是的。您需要做的就是创建一个 NSDictionary
,例如:
var dict:[NSString : UIImage] = ["image" : yourImage]
然后您只需使用以下方法在 Apple Watch 和 iOS 应用程序之间进行通信:
是的,你可以做到。
您可以使用此方法传递数据
- (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
}
你可以用这种方法处理你的数据。
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
UIImage *image = [userInfo objectForKey:@"image"];
NSDictionary *replyInfo = @{@"success":@"success",@"error":@""};
reply(replyInfo);
}
否则请参阅本教程
这里的答案只对了一半,因为你不能直接发送 UIImage!
您需要将图像作为 NSData 发送,例如 f.e。所以:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply
{
reply(@{@"image" : UIImagePNGRepresentation(yourUIImage)});
// JPEG Representation would of course also work
}
我有一个 iPhone 应用程序可以在 5-10 秒后将 UIView 捕获为 UIImage。是否可以将该 UIImage 传递到我的 Apple Watch 应用程序并在 UIImageView 中显示它?
是的。您需要做的就是创建一个 NSDictionary
,例如:
var dict:[NSString : UIImage] = ["image" : yourImage]
然后您只需使用以下方法在 Apple Watch 和 iOS 应用程序之间进行通信:
是的,你可以做到。
您可以使用此方法传递数据
- (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
}
你可以用这种方法处理你的数据。
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
UIImage *image = [userInfo objectForKey:@"image"];
NSDictionary *replyInfo = @{@"success":@"success",@"error":@""};
reply(replyInfo);
}
否则请参阅本教程
这里的答案只对了一半,因为你不能直接发送 UIImage! 您需要将图像作为 NSData 发送,例如 f.e。所以:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply
{
reply(@{@"image" : UIImagePNGRepresentation(yourUIImage)});
// JPEG Representation would of course also work
}