2015-04-24 27 views
0

我一直以來有代碼在我的iPhone應用程序的一個片段,試圖掩蓋一些JPEG圖像的白色背景。今天,這條消息在iPhone上仍然運行良好,但並不掩蓋Apple Watch。該代碼在預期成功(非NULL)路徑上運行,但不執行屏蔽。即使我的maskingColors陣列改變爲範圍0.0, 255.0每個組件,沒有掩蔽,進行當在WKInterfaceImage顯示(具有setImage:)(在iPhone上完全掩蓋圖像此相同的變化)。CGImageCreateWithMaskingColors不能在Apple關注面具WKInterfaceImage

與存儲在資產目錄alpha通道PNG圖像似乎在WKInterfaceImage到蘋果觀看正常顯示。

CGImageCreateWithMaskingColors不是Apple關注安全嗎?

- (UIImage *)imageWithBackgroundRemovedWithImageData:(NSData *)imageData 
{ 
    CGImageRef originalImage = [UIImage imageWithData:imageData].CGImage; 

    /* Only attempt for RGB images */ 
    if (CGColorSpaceGetModel(CGImageGetColorSpace(originalImage)) != kCGColorSpaceModelRGB) 
     return ([UIImage imageWithData:imageData]); 

    /* Mask 10 shades of "white" */ 
    static const CGFloat maskingColors[] = {245.0, 255.0, 245.0, 255.0, 245.0, 255.0}; 
    CGImageRef transparentImageRef = CGImageCreateWithMaskingColors(originalImage, maskingColors); 
    if (transparentImageRef == NULL) 
     return ([UIImage imageWithData:imageData]); 

    UIImage *transparentImage = [UIImage imageWithCGImage:transparentImageRef]; 
    CGImageRelease(transparentImageRef); 
    return (transparentImage); 
} 
+0

您可以添加Watch代碼(即如何編碼和設置圖像)? –

+0

@MikeSwanson我簡單地在'WKInterfaceTable'的一行中用'WKInterfaceImage'返回發佈的代碼來調用'setData'。我發送的'NSData'從iPhone應用程序以JPEG形式從網站下載,並從'openParentApplication'的'reply'塊傳輸到手錶。我也嘗試在手機上執行屏蔽步驟,並將產生的'UIImagePNGRepresentation()'發送到手錶,結果相同。 – greg

+0

更正:setImage,顯然不是setData。 – greg

回答

1

這似乎是一個已經存在一個問題,因爲iOS的7。看到這個職位的詳細信息:CGImageCreateWithMaskingColors Doesn't Work with iOS7

根據這種邏輯,我已經修改了你的代碼如下產生預期的結果:

UIGraphicsBeginImageContextWithOptions(transparentImage.size, NO, 1.0); 
[transparentImage drawInRect:CGRectMake(0, 0, transparentImage.size.width, transparentImage.size.height)]; 
UIImage *anotherRendition = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return (anotherRendition); 
+0

我將'UIGraphicsBeginImageContextWithOptions'的'scale'參數設置爲0而不是1,但這確實有效。雖然確實是一個錯誤。 – greg