我有一个SKSpriteNode:
private var btnSound = SKSpriteNode(imageNamed: "btnSound")
现在我用 Adobe Illustrator 制作了这张图片,大小为 2048x2048 像素(实际上太大了),因此它具有很好的分辨率。我的问题是,当我设置图像大小时,其中的线条会变得锯齿状或不平滑。
我是这样调整大小的:
btnSound.position = CGPoint(x: self.frame.width * 1 / 5 , y: self.frame.height * (5.2 / 8))
btnSound.size.width = self.frame.width * 1 / 7
btnSound.size.height = btnSound.size.width
btnSound.zPosition = 1
self.addChild(btnSound)
这是Illustrator软件中的图像(截图)
![image](https://istack.dev59.com/5YSIy.webp)
![image](https://istack.dev59.com/aGo1J.webp)
- Making the image PDF
- Making the image PNG
- Making the PNG 72 DPI, making it 300 DPI
- Run on simulator / device (iPhone7)
btnSound.setScale(preDetermineScale)
Using the following function, though I am not familiar with the UIGraphicsBeginImageContext method. The image just comes out blurry with this. Heres the code and the resulting image:
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage? { let scale = newWidth / image.size.width let newHeight = image.size.height * scale UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } func setup() { let btnSoundImg = UIImage(named: "btnSound") let resizeBtnSoundImage = resizeImage(image: btnSoundImg!, newWidth: self.frame.width * 1 / 7) let btnSoundTexture = SKTexture(image: resizeBtnSoundImage!) btnSound.texture = btnSoundTexture btnSound.position = CGPoint(x: self.frame.width * 1 / 5 , y: self.frame.height * (5.2 / 8)) btnSound.size.width = self.frame.width * 1 / 7 btnSound.size.height = btnSound.size.width btnSound.zPosition = 1 self.addChild(btnSound) }
我是个自学者,没有做过太多编程方面的工作,所以我很想学习如何正确地完成这项任务,因为我只能找到调整 UIImageView 大小的解决方案。
我还想到一个想法,也许它不应该是一个 spriteNode,因为它只用于按钮?