调整SKSpriteNode的大小而不失去质量

5

我有一个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,这是应用程序中的图像(截图)image 我尝试过的方法:
  • 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,因为它只用于按钮?


为什么不直接在应用程序中所需的大小上制作图像呢? - Nik
似乎那并不能解决问题。我只是把它们放大,这样如果以后需要在更高的分辨率下使用它们,我可以调整它们。此外,它们将根据设备类型而有所不同。 - Steve
2
btnSound图片的扩展名是什么(添加到您的项目中的那个)?此外,我建议在SpriteKit中坚持使用sprites。使用UIKit元素将需要一些额外的工作,例如,在转换到另一个场景时,您将不得不手动删除UIKit元素,因为这些元素是添加到视图而不是场景中的节点(与节点树的一部分不同)。 - Whirlwind
1
如果您的图像不是矢量图,而是位图,则拥有那么大的图像(2048x2048)然后将其缩小,只会浪费资源。 - Whirlwind
这样的图像可能可以通过矢量图形来完成,苹果技术上支持这种方式。(它将在编译时渲染png文件) - Knight0fDragon
1个回答

10

首先,有一些基本规则需要遵循,以获得最佳的结果。

  1. 只按2的倍数进行缩放,例如50%,25%,12.5%,6.25%等。

    这样,在每次缩小尺寸时,原始图像中的任何四个像素就会变成缩放后图像中的1个像素。

  2. 将原始图像调整为2的幂次方大小的正方形。因此:128x128、256x256、512x512等等。你已经通过2048x2048的尺寸满足了此要求。

  3. 开启mipmapping。在SpriteKit中,默认情况下是关闭的,因此您必须打开它:https://developer.apple.com/reference/spritekit/sktexture/1519960-usesmipmaps

  4. 尝试不同的过滤模式,以获得图像中噪点和带状的最佳缩减效果:https://developer.apple.com/reference/spritekit/sktexture/1519659-filteringmode,提示:线性可能更好。

  5. 一如既往地,善用Photoshop手动缩放将给您最佳的结果和最少的灵活性。


感谢您的热情回复。我正在看mipmapping,但以前没有使用过。您能否给出一个关于它如何工作的示例,或者对于这个特定的问题来说可能有些复杂? - Steve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接