I have an UIImageView, which shows an UIImage, having content mode as Aspect Fit.
I need to indicate transparent areas of the images with a chessboard. How to obtain squares of the same size, no matter the size of the image?
extension UIImage {
func withChessboard() -> UIImage? {
var image: UIImage?
let l = 4
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(UIColor.lightGray.cgColor)
for row in 0 ..< Int(size.width) {
for col in 0 ..< Int(size.height) {
if (row + col) % 2 == 0 {
context.fill(CGRect(x: col * l, y: row * l, width: l, height: l))
}
}
}
draw(in: imageRect, blendMode: .normal, alpha: 1.0)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
return nil
}
}
Now I have this ( first image being smaller )
and this:
EDIT, using the solution suggested by Trent, I have obtained this. I need the tiles to cover just the size of the image, not the whole imageview.