I have a large Tilesheet (over 1000 tiles in one picture) like this:
Each tile is 64x64 and I want to split the picture. in this example I would be 4 tiles and put in an array. How can I do this? with different sizes? I need some magic (math) or better an idea how can I get the right position to crop in the loop.
Some fast coded Code:
func cropImage(image: UIImage, tileSize: Int) -> [UIImage]? {
if Int(image.size.height) % 64 != 0 && Int(image.size.width) % 64 != 0 {
return nil
}
let hCount = Int(image.size.height) / tileSize
let wCount = Int(image.size.width) / tileSize
var tiles:[UIImage] = []
for i in 0...hCount {
for p in 0...wCount {
let temp:CGImage = image.cgImage!.cropping(to: CGRect.zero)!
tiles.append(UIImage(cgImage: temp))
}
}
return tiles
}