在我的iPhone應用程序,我檢索圖像,並將它們保存到文件目錄,以便更快加載。我知道要在Apple Watch上使用這些圖像,我必須與App Group分享這些圖像。
因此,我創建了一個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"];
- 注:我的文件管理類返回一個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];
Q問題:
當我在Apple Watch上測試時沒有圖像顯示。爲了在我的應用和Apple Watch之間保存/檢索圖像,我需要做些什麼不同?
您是否使用watchOS 1或2? watchOS 2無法訪問共享的應用程序組。 – dan
我正在使用watchOS 2 ....多數民衆贊成在奇怪的,你能否建議另一種解決方案使用保存在手錶的應用程序的文檔目錄中的圖像? –
您必須使用WatchConnectivity框架並在WCSession上使用'transferFile:metadata:'方法將文件從手機傳輸到手錶,然後將該文件保存在手錶自己的文檔目錄中。 – dan