Created
March 2, 2023 05:40
-
-
Save codelynx/3ad8fc56fe014ee0a42ea3d8ca33b4d3 to your computer and use it in GitHub Desktop.
UIImage extension to create 1 pixel color image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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