在Swift中用纯色创buildUIImage
我想以编程方式创build一个纯色的UIImage填充。 任何人都有一个想法如何在Swift中做到这一点?
另一个很好的解决scheme, Swift 2.2兼容,是在UIImage中创build另一个构造函数,这样:
public extension UIImage { public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) { let rect = CGRect(origin: .zero, size: size) UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) color.setFill() UIRectFill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() guard let cgImage = image?.CGImage else { return nil } self.init(CGImage: cgImage) } }
通过这种方式,您可以通过以下方式创build自定义彩色图像:
let redImage = UIImage(color: .redColor())
或者,也可以使用自定义大小创build图像:
let redImage200x200 = UIImage(color: .redColor(), size: CGSize(width: 200, height: 200))
Swift 3.0
public extension UIImage { public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) { let rect = CGRect(origin: .zero, size: size) UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) color.setFill() UIRectFill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() guard let cgImage = image?.cgImage else { return nil } self.init(cgImage: cgImage) } }
这是另一种select。 我相信你想要一个确切的UIImage对象。
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRectMake(0, 0, size.width, size.height) UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }
坚持这个你的Swift代码,并调用它
Swift 3.1:
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image }
class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imageView.backgroundColor = UIColor.redColor() } }
类似的方法,如果你想绘制图像自己与通过IBOutlet连接。
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var frame = CGRectMake(100,100,100,100) var imageView2 = UIImageView(frame: frame) imageView2.backgroundColor = UIColor.redColor() self.view.addSubview(imageView2) } }
从anthonyliao借来的第三种方法。 更复杂一点:
class ViewController: UIViewController { func getImageWithColor(color: UIColor, size: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(CGRectMake(0, 0, 100, 100)) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } override func viewDidLoad() { super.viewDidLoad() var imageView = UIImageView(frame: CGRectMake(100,100,100,100)) let screenImage = getImageWithColor(UIColor.redColor(), size: CGSize(width: 100, height: 100)) imageView.image = screenImage self.view.addSubview(imageView) } }
一个更清洁的方法是将这些逻辑封装在UIImage
扩展中:
import UIKit extension UIImage { class func imageWithColor(color: UIColor) -> UIImage { let rect: CGRect = CGRectMake(0, 0, 1, 1) UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), false, 0) color.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }
现在消费者可以调用UIImage.imageWithColor(UIColor.blackColor())
来创build一个黑色背景的图像。
Swift 3版本的@anthonyliao接受的答案:
class func getImageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: size.width, height: size.height)) UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image }
您可以使用新的iOS 10 UIGraphicsImageRenderer API。
这里是在Swift 3.1中对UIColor的扩展
extension UIColor { func getImage(size: CGSize) -> UIImage { let renderer = UIGraphicsImageRenderer(size: size) return renderer.image(actions: { rendererContext in self.setFill() rendererContext.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) }) }}
一个不错的方法是有一个像这样的计算属性:
extension UIColor { var imageRepresentation : UIImage { let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context?.setFillColor(self.cgColor) context?.fill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } }
用法:
let redImage = UIColor.red.imageRepresentation