如何将UIImage从iPhone应用程序传递到Apple Watch应用程序

时间:2015-04-08 13:10:24

标签: objective-c iphone swift watchkit apple-watch

我有一个iPhone应用程序,在5-10秒后将UIView捕获为UIImage。是否可以将UIImage传递给我的Apple Watch应用程序并在UIImageView中显示它?

3 个答案:

答案 0 :(得分:4)

这里的答案只有一半是正确的,因为你无法直接发送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
}

答案 1 :(得分:3)

是的。您需要做的就是创建NSDictionary之类的:

var dict:[NSString : UIImage] = ["image" : yourImage]

然后您只需使用以下方法在Apple Watch和iOS应用之间进行通信: LINK

答案 2 :(得分:0)

是的,你可以做到。

您可以使用此方法传递数据

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

否则请参阅本教程

MMWormhole