我有一個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)
事情我已經嘗試在應用程序(截圖)圖片:
- 使圖像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) }
我是自學的,並沒有正確的做了一大堆的編程,所以我很想學習如何做到這一點我只找到調整UIImageViews的解決方案。
我的另一個想法是也許它不應該是一個spriteNode,因爲它只是用於按鈕?
你爲什麼不只是讓你的圖像大小,這將是明智地使用的Photoshop在你的應用程序? – Nik
這似乎不能解決它。我只是讓它們變大,所以如果我以後需要更高分辨率的話我可以調整它們。此外,它們將根據設備類型而變化。 – Steve
btnSound圖像有什麼擴展名(添加到您的項目中)?另外,我建議在SpriteKit中堅持使用精靈。使用UIKit元素將需要一些額外的工作,例如。當轉換到另一個場景時,您將不得不手動刪除UIKit元素,因爲這些元素被添加到視圖中,而不是添加到場景中(與屬於節點樹一部分的節點不同)。 – Whirlwind