我尝试使用一个扩展程序获取彩色图像(star.jpg,只是一个星形图标)。结果,我得到整个UIImageView
的颜色,包括我的形象。我怎么才能得到UIImage
彩色?
以下是我使用的代码: 的UIImage + Tinting.swift
import UIKit
extension UIImage {
func tintWithColor(color:UIColor)->UIImage {
UIGraphicsBeginImageContext(self.size)
let context = UIGraphicsGetCurrentContext()
// Flip the image
CGContextScaleCTM(context, 1.0, -1.0)
CGContextTranslateCTM(context, 0.0, -self.size.height)
// Multiply blend mode
CGContextSetBlendMode(context, CGBlendMode.Multiply)
let rect = CGRectMake(0, 0, self.size.width, self.size.height)
CGContextClipToMask(context, rect, self.CGImage)
color.setFill()
CGContextFillRect(context, rect)
// Create uiimage
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let myImageview = UIImageView()
let myImage = UIImage(named: "star.jpg")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myImageview.image = myImage
let img = myImageview.image
myImageview.image = img?.tintWithColor(UIColor.redColor())
// Set position of the image view
myImageview.translatesAutoresizingMaskIntoConstraints = false //Don't forget this line
view.addSubview(myImageview)
let horizontalConstraint = NSLayoutConstraint(item: myImageview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: myImageview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(verticalConstraint)
let widthConstraint = NSLayoutConstraint(item: myImageview, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 0.5, constant: 0)
view.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: myImageview, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 0.3, constant: 0)
view.addConstraint(heightConstraint)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
使用以下代码使用tintColor更改UIImageView图像的外观。还提供了Alpha通道的样本图像。
// *** I assume you have define IBOutlet of your imageView or programatically created ***
@IBOutlet weak var ivStar: UIImageView!
// *** Set `UIImage` with rendering mode `AlwaysTemplate`
ivStar.image = UIImage(imageLiteral: "Star").imageWithRenderingMode(.AlwaysTemplate)
// *** Set TintColor on `UIImageView` as per your Appearance requirement ***
ivStar.tintColor = UIColor.blueColor();
带有Alpha PNG的样本星形图标
答案 1 :(得分:0)
这样做:
<tr>
<td>Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Select City:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
</td>
</tr>
<强>更新强>
尝试复制你的确切设置,没有扩展中的上下文绘图,并进行基于扩展的色调(并非它非常聪明,在函数中已经作为swift的标准实现了,但只是为了它的乐趣)
let image = UIImage(named: "yourimage");
let tintedImage = image!.imageWithRenderingMode(.AlwaysTemplate)
let imageView = UIImageView(frame: self.view.frame)
imageView.image = tintedImage
view.addSubview(imageView)
imageView.tintColor = UIColor.redColor()