寫技術文

想把一些摸索的過程與結果作個紀綠。主要還是給以後的自己方便查找。用Blogger寫技術文需要一個好的呈現程式碼的方式。於是參考了這一篇。效果如下: 

  func getItemTexture(from imagePath: String) {
        let textureAtlas = SKTexture(imageNamed: imagePath)
        let width = (textureAtlas.size().width)
        let height = (textureAtlas.size().height)
        
        for item in items {
            let textureRect = CGRect(x: item.position.x/width, y: 1-item.position.y/height - item.size.height / height , width: item.size.width/width, height: item.size.height / height)
            
            DispatchQueue.main.async { [unowned self] in
                if let index = self.items.firstIndex(where: {$0.name == item.name}) {
                    self.items[index].uiImage = UIImage(cgImage: SKTexture(rect: textureRect, in: textureAtlas).cgImage())
                    
                }
            }
        }
        
    }
效果還不錯哩!

Comments

Popular Posts