ios - The way to resize a UIImage with out antialiasing?

von Satoshi Nakamoto

ios - The way to resize a UIImage with out antialiasing?

I'm growing an iOS board sport. I'm attempting to present the board a form of "texture".



What I did was I created this very small picture (actually small, be sure you look fastidiously):



enter image description here



And I handed this picture to the UIColor.init(patternImage:) initializer to create a UIColor that's this picture. I used this UIColor to fill some sq. UIBezierPaths, and the consequence seems to be like this:



enter image description here



All copies of that picture strains up completely they usually kind many diagonal straight strains. To date so good.



Now on the iPad, the squares that I draw shall be bigger, and the borders of these squares shall be bigger too. I've efficiently calculated what the stroke width and measurement of the squares ought to be, so that isn't an issue.



Nevertheless, for the reason that squares are bigger on an iPad, there shall be extra diagonal strains per sq.. I are not looking for that. I must resize the very small picture to an even bigger one, and that the dimensions depends upon the stroke width of the squares. Particularly, the width of the resized picture ought to be twice as a lot because the stroke width.



I wrote this extension to resize the picture, tailored from this publish:



extension UIImage
func resized(toWidth newWidth: CGFloat) -> UIImage

let scale = newWidth / measurement.width
let newHeight = measurement.peak * scale
UIGraphicsBeginImageContextWithOptions(CGSize(width: newWidth, peak: newHeight), false, 0)
self.draw(in: CGRect(x: 0, y: 0, width: newWidth, peak: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!



And known as it like this:



// that is the code I used to attract a single sq.
let path = UIBezierPath(rect: CGRect(origin: level(for: Place(x, y)), measurement: CGSize(width: squareLength, peak: squareLength)))
UIColor.black.setStroke()
path.lineWidth = strokeWidth
// that is the road that is essential!
UIColor(patternImage: #imageLiteral(resourceName:


"texture").resized(toWidth: strokeWidth * 2)).setFill()
path.fill()
path.stroke()



Now the sport board seems to be like this on an iPhone:



enter image description here



You would possibly must zoom within the webpage a bit to see what I imply. The board now seems to be extraordinarily ugly. You'll be able to see the "borders" of every copy of the picture. I do not need this. On an iPad although, the board seems to be fantastic. I believe that this solely occurs once I downsize the picture.



I figured that this is perhaps because of the antialiasing that occurs once I use the extension. I discovered this publish and this publish about eradicating antialiasing, however the former appears to be doing this in a picture view whereas I'm doing this within the draw(_:) technique of my customized GameBoardView. The latter's answer appears to be precisely the identical as what I'm utilizing.



How can I resize with out antialiasing? Or on a better degree of abstraction, How can I make my board look fairly?





Source link

Read the full article
Porträt von Satoshi Nakamoto

Satoshi Nakamoto

Zur Person

Satoshi Nakamoto