Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created March 2, 2023 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/3ad8fc56fe014ee0a42ea3d8ca33b4d3 to your computer and use it in GitHub Desktop.
Save codelynx/3ad8fc56fe014ee0a42ea3d8ca33b4d3 to your computer and use it in GitHub Desktop.
UIImage extension to create 1 pixel color image
import UIKit
extension UIImage {
convenience init(color: UIColor) {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1))
let image = renderer.image { context in
context.cgContext.setFillColor(UIColor.blue.cgColor)
context.cgContext.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
}
guard let cgImage = image.cgImage else { fatalError() }
self.init(cgImage: cgImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment