2015-07-06 33 views
0

我想要做的是将具有纯白色背景的this图标转换为具有混合背景的this图标。在Swift中应用混合模式

图标背后的背景并不重要,我试图实现的是应用于图标的效果。该图标是通过在Photoshop中使用Soft Light混合模式创建的。我想在iOS中重新创建。

这是我尝试过,通过this comment启发代码:

@IBOutlet weak var cogButton: UIButton! 

func applyCogBackground() { 
    UIGraphicsBeginImageContextWithOptions(cogButton.frame.size, false, 1) 
    var context = UIGraphicsGetCurrentContext() 
    view.drawViewHierarchyInRect(cogButton.bounds, afterScreenUpdates: false) 
    UIImage(named: "Cog")?.drawInRect(cogButton.bounds, blendMode: kCGBlendModeSoftLight, alpha: 1) 
    var result = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    cogButton.setBackgroundImage(result, forState: .Normal) 

} 

我使用drawHierarchyInRect(),而不是CALayer.renderInContext(),因为我已经告诉它快得多性能明智的。无论如何,无论哪种方式都会导致相同的结果。该图标没有改变(它保留了纯白的背景)。我究竟做错了什么?

回答

0

您可以尝试为按钮的图像应用不同的呈现模式。

cogButton.setBackgroundImage(UIImage(named:"Cog")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal) 
+0

不幸的是,只是结果[this](http://i.imgur.com/i2rEPFG.png):(谢谢你试试。 – Nexuist