2017-06-14 84 views
1

好的。這真的很奇怪。我正在嘗試將手機應用中的顏色傳輸到其伴侶手錶應用。從手機發送UIColor以奇怪的顏色觀看結果

標準(UIColor.red等)顏色工作,但由IB文件定義的顏色不工作。我得到奇怪的顏色,這好像是一個色彩空間問題。

First, here's a VERY simple (and absolutely HIDEOUS) app project that demonstrates this.

我在做什麼,在發送從監視的消息來了電話,詢問電話的一組顏色。

電話通過編譯UIColor實例列表並將它們發送到手錶來實現責任。它通過首先實例化三種標準顏色(UIColor.red,UIColor.green,UIColor.blue),然後附加一些從應用程序故事板中定義的標籤列表中讀取的顏色(它從標籤中讀取文本顏色)。

然後將由此產生的7個UIColor實例組成的數組序列化併發送到手錶,並將其反序列化,並創建一個簡單的標籤表;每個都有相應的顏色。

前三個顏色好看:

The first three (Standard) colors are golden

然而,當我讀到來自IB的顏色,他們得到傾斜。

這裏是什麼應該發生:

These colors should be exactly matched on the Watch

這裏到底發生了什麼:

The colors are wrong

只有最後一個(奇數)的顏色是正確的。顏色在故事板中被定義爲RGB(滑塊)。

Here's the code in the iOS app that gathers and sends the color

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { 
    print("iOS App: session(_:,didReceiveMessage:)\n\(message)") 
    if let value = message["HI"] as? String { 
     if "HOWAYA" == value { 
      var colorArray: [UIColor] = [UIColor.red, UIColor.green, UIColor.blue] 
      if let primaryViewController = self.window?.rootViewController { 
       if let view = primaryViewController.view { 
        for subview in view.subviews { 
         if let label = subview as? UILabel { 
          if let color = label.textColor { 
           colorArray.append(color) 
          } 
         } 
        } 
       } 
      } 
      let colorData = NSKeyedArchiver.archivedData(withRootObject: colorArray) 
      let responseMessage = [value:colorData] 

      session.sendMessage(responseMessage, replyHandler: nil, errorHandler: nil) 
     } 
    } 
} 

Here's the code in the Watch that gets the colors, and instantiates the labels

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { 
    if let value = message["HOWAYA"] as? Data { 
     if let colorArray = NSKeyedUnarchiver.unarchiveObject(with:value) as? [UIColor] { 
      self.labelTable.setNumberOfRows(colorArray.count, withRowType: "TestColorTableRow") 
      for row in 0..<colorArray.count { 
       if let controller = self.labelTable.rowController(at: row) as? TestTableRowController { 
        controller.setLabelColor(colorArray[row]) 
       } 
      } 
     } 
    } 
} 

我送(在另一個應用程序),通過細變,但顏色偏其他數據。

任何想法?

更新:有人建議,也許序列化是一個問題,所以我分支演示,並且仍然在iOS中反序列化。事實證明,確定。

I created this branch,在那裏我發送消息之前反序列化:

func colorCrosscheck(_ value: Data) { 
    if let colorArray = NSKeyedUnarchiver.unarchiveObject(with:value) as? [UIColor] { 
     if let primaryViewController = self.window?.rootViewController { 
      if let view = primaryViewController.view { 
       for subview in view.subviews { 
        if let containerView = subview as? ViewList { 
         for row in 3..<colorArray.count { 
          if let label = containerView.subviews[row - 3] as? UILabel { 
           label.textColor = colorArray[row] 
          } 
         } 
         break 
        } 
       } 
      } 
     } 
    } 
} 

我所做的,是添加四個標籤清晰文本顏色,是由我的手錶上使用相同的過程着色。

手錶收到信息幾秒鐘後,iOS中的頂部下方會出現四個標籤。

結果表明序列化不是問題:

Looks OK on iOS

UPDATE(2):有人建議我嘗試以編程方式添加顏色。這些工作正常,這意味着這看起來像一個蘋果的錯誤。當我收到我的DTS事件迴應時,我會報告回來。

First, here's a new branch that demonstrates adding the colors dynamically.

Here's the relevant section of code(I還與IB文件弄亂):

colorArray.append(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0)) 
colorArray.append(UIColor(red: 1.0, green: 0.5, blue: 0.0, alpha: 1.0)) 
colorArray.append(UIColor(red: 0.0, green: 0.0, blue: 0.5, alpha: 1.0)) 
colorArray.append(UIColor(hue: 1.2, saturation: 1.0, brightness: 0.5, alpha: 1.0)) 
colorArray.append(UIColor(hue: 1.2, saturation: 0.6, brightness: 0.7, alpha: 1.0)) 

我加入5種新的動態顏色,在RGB和HSL。

它們顯示了罰款,在iOS應用:

Looks Good on iOS (If you're a deranged magpie)

而且他們還正確地傳送到手錶:

The first Three The Last Two

+1

你確定你的數據編碼和解碼是正確的嗎?我會嘗試解除電話本身的數據並查看顏色是否仍然正確。我看不出有什麼明顯的代碼問題,所以這是目前我能想到的唯一bug。 –

+0

可能就是這樣。我會在午餐時間測試一下。 –

+0

我剛測試過它。在iOS上運行良好。我會用我的研究來更新這個問題。 –

回答

0

確定。我有一個解決方法。這絕對是一個bug,但我不能等到iOS 11/WatchOS 4出來。這種解決方法是有效的(到目前爲止 - 需要更多的測試)。

Here's the branch with the workaround.

if let color = label.textColor { 
    if let destColorSpace: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB) { 
     let newColor = color.cgColor.converted(to: destColorSpace, intent: CGColorRenderingIntent.perceptual, options: nil) 
     colorArray.append(UIColor(cgColor: newColor!)) 
    } 
} 

它看起來像基於IB色的核心顏色是不好的。不知道它是如何得到正確的顏色。

我所做的是從原始的CGColor創建一個新的CGColor,並將其轉換爲sRGB。新的CGColor是有效的。

這是一個糟糕的問題,但它應該可以正常工作。

雖然我需要測試內存泄漏。 CG傾向於像篩子一樣泄漏。

+0

好的。看起來這是一個很好的解決方法。沒有泄漏。 –

+0

嗯......我得到了我的第一級「懶惰CS代表」的答案,他們甚至沒有看我的項目,並建議我做我一直在做的事情。我反彈它,我們會看到接下來會發生什麼。 –

+0

@DávidPásztor我終於得到了明確的答案。對。這是一個錯誤。他們實際上並沒有這樣說。相反,他們說「問題#32790627仍在調查中」,並將我的TSI交還給我。所以,是的,這是一個錯誤,並且在罕見的情況下,只要使用色彩空間轉換技巧來解決它。 –