gpt4 book ai didi

iOS:将图像保存在 App Group 中以便在 Apple Watch 上使用

转载 作者:行者123 更新时间:2023-12-01 17:29:42 32 4
gpt4 key购买 nike

背景:

在我的 iPhone 应用程序中,我检索图像并将它们保存到文档目录以便更快地加载。我知道要在 Apple Watch 上使用这些图像,我必须与 App Group 共享它们。

因此,我创建了一个应用程序组,更新了我的配置文件,所有这些爵士乐。现在我的问题是我不知道如何将图像保存到 App Group 并在我的 WatchKit 文件中读取该图像。

这是我尝试将图像保存到应用程序组的方法:

NSString *container = @"group.com.appName.watchdatasharing";

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

[defaults setValue:UIImagePNGRepresentation([FileManager readImageFromFileWithName:@"icon1_imgUrl"]) forKey:@"icon1_imgUrl"];
  • 注意:我的 FileManager 类返回一个 UIImage

为了在我的 WatchKit 应用程序中检索图像,我使用了以下代码:

NSString *container = @"group.com.fantrac.watchdatasharing";

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

NSData* imageData = [defaults valueForKey:@"icon1_imgUrl"];
UIImage* image = [UIImage imageWithData:imageData];

[tableRow.iconImage setImage:image];

问题:

当我在 Apple Watch 上测试时没有图像显示。我需要做哪些不同的事情才能在我的应用程序和 Apple Watch 之间保存/检索图像?

最佳答案

如果您使用 watchOS 2,则可以使用 WatchConnectivity。我附上示例代码供您引用。

在 iPhone 上:

// Create a Watch Connectivity session
- (void)viewDidLoad
{
[super viewDidLoad];

self.applicationDict = @{@"foo" : @"bar"};

if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}

// Transfer file to Apple Watch
- (IBAction)fileTransferButtonTapped:(id)sender
{
// File Transfer
NSURL *url =
[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"]];
WCSessionFileTransfer *fileTransfer =
[[WCSession defaultSession] transferFile:url
metadata:self.applicationDict];
}

在 watch 上:

// Receive file from iPhone
- (void)session:(nonnull WCSession *)session didReceiveFile:(nonnull WCSessionFile *)file
{
// recieve file
}

引用。 http://www.kristinathai.com/watchos-2-how-to-communicate-between-devices-using-watch-connectivity/

关于iOS:将图像保存在 App Group 中以便在 Apple Watch 上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178793/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com