2016-12-15 23 views
4

我有一個SKSpiteNode:調整大小的SKSpriteNode而不失品位

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

現在我在Adobe Illustrator製作這種圖像大小爲2048×2048像素(矯枉過正真的),因此具有良好的分辨率。我的問題是,當我設置圖像的大小,其中的線條鋸齒或鋸齒...不光滑。

這是我的尺寸是:

 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,因爲它只是用於按鈕?

+0

你爲什麼不只是讓你的圖像大小,這將是明智地使用的Photoshop在你的應用程序? – Nik

+0

這似乎不能解決它。我只是讓它們變大,所以如果我以後需要更高分辨率的話我可以調整它們。此外,它們將根據設備類型而變化。 – Steve

+1

btnSound圖像有什麼擴展名(添加到您的項目中)?另外,我建議在SpriteKit中堅持使用精靈。使用UIKit元素將需要一些額外的工作,例如。當轉換到另一個場景時,您將不得不手動刪除UIKit元素,因爲這些元素被添加到視圖中,而不是添加到場景中(與屬於節點樹一部分的節點不同)。 – Whirlwind

回答

7

首先,要遵循一些原始規則以獲得最佳結果。

  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. 由於歷來的情況下,手動縮放會給你最好的結果和最靈活

+0

這是我的書籤... – Fluidity

+0

感謝您的迴應。我正在看一下mipmapping,但之前沒有使用它。你能舉一個例子說明它是如何工作的,或者對於這個特定的問題是否涉及太多? – Steve