Resize SKSpriteNode without quality loss

I have SKSpiteNode:

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

Now I made this image in Adobe Illustrator with a size of 2048x2048 pixels (actually crowded), so it has a good resolution. My problem is that I set the image size, the lines in it are jagged or jagged ... not smooth.

Here's how I size it:

        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)

This is the image when in Illustrator (screenshot) imageand this is the image in the application (screenshot)image

Things I tried:

  • Create PDF Image
  • Create PNG image
  • Create PNG 72 DPI, making it 300 DPI
  • Launch on simulator / device (iPhone7)
  • btnSound.setScale(preDetermineScale)
  • Using the following function, although I am not familiar with the UIGraphicsBeginImageContext method. The image is just blurry with this. Here is 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)
    }
    

blurred image

, , , UIImageViews.

, , , spriteNode, ?

+4
1

, , , .

+8

Source: https://habr.com/ru/post/1663792/


All Articles