ios - 在不损失质量的情况下调整 SKSpriteNode 的大小

标签 ios sprite-kit swift3

我有一个 SKSpiteNode:

private var btnSound = SKSpriteNode(imageNamed: "btnSound")

现在,我在 Adob​​e 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

我尝试过的事情:

  • 制作图片PDF
  • 制作PNG图片
  • 制作 PNG 72 DPI,制作 300 DPI
  • 在模拟器/设备(iPhone7)上运行
  • btnSound.setScale(preDetermineScale)
  • 使用以下函数,虽然我不熟悉 UIGraphicsBeginImageContext 方法。图像就这样模糊了。下面是代码和生成的图像:

    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)
    }
    

blurry image

我是自学的,没有做过很多编程,所以我很想学习如何正确地做到这一点,因为我只是在寻找调整 UIImageViews 大小的解决方案。

我的另一个想法是,也许它不应该是一个 spriteNode,因为它只是用于一个按钮?

最佳答案

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

  1. 仅按 2 倍缩放。即 50%、25%、12.5% 6.25% 等。

    这样一来,原始图像中的任意四个像素在缩放后的图像中变为 1 个像素,比例尺寸每减小一步。

  2. 将您的原始图像设为指数为 2 的正方形。因此:128x128、256x256、512x512 等。您已经使用 2048x2048 大小调整了这一点。

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

  4. 尝试使用不同的过滤模式以获得图像中噪声和 strip 的最佳减少:https://developer.apple.com/reference/spritekit/sktexture/1519659-filteringmode提示,线性可能会更好。

  5. 一如既往,明智地使用 Photoshop 进行手动缩放会给您带来最好的结果和最少的灵 active

关于ios - 在不损失质量的情况下调整 SKSpriteNode 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41154948/

相关文章:

iphone - iOS将实体从类添加到核心数据

ios - 使用 UILabels 和 UITableViewCell 创建自定义 UIActionSheet/UIView

ios - 如何在不同的 SpriteKit 场景中显示 iAd 横幅

ios - 创建一个分割 map Swift

ios - 用户默认保存开关状态

ios - InAppSettingsKit - 能够选择多个值

sprite-kit - 将 SKLabelNode 置于 SKSpriteNode 的中心

ios - 如何使用带有 Alamofire 参数的数组?

ios - 创建日历,即使关闭 iCloud 也没有本地资源

ios - 如何设置 Xamarin iOS UITableView 重新排序图标颜色