How do you merge a sequence of images into one texture

How do you merge a sequence of images into one texture

If I understand what you're looking for correctly, you can add all images as SKSpriteNodes to one SKNode, and then call SKView.textureFromNode which will provide you with one texture. You can then clean up the individual SKSpriteNodes.


https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/#//apple_ref/occ/instm/SKView/textureFromNode:

I've tried your suggestion but i realize i have to render the SKNode to screen before i can get the texture from it. I dont want to do that.

It's fairly easy to combine images by drawing into an image context:


UIGraphicsBeginImageContextWithOptions (contextRect.size, false, 0)
let context = currentCGContext
// Flip the context if necessary
if isFlipped {
     CGContextTranslateCTM (context, 0, contextRect.height)
     CGContextScaleCTM (context, 1, -1)
}
// Draw the subimages
     subimage.drawInRect (drawRect) // etc
// Create the texture
let contextImage = UIGraphicsGetImageFromCurrentImageContext ()
UIGraphicsEndImageContext ()
let texture = SKTexture (image: contextImage)


Note that specifying 0 as the last parameter to UIGraphicsBeginImageContextWithOptions is what gives you a retina-resolution result

func mergeImages()->SKTexture {

let texture = UIImage(named: "bolt_1")

let numOfHorTiles : Int = Int(ceil(size.width / texture!.size.width))

var newImage : UIImage

let contextRect = CGRect(x: 0, y: 0, width: gameLogic.screenSizeRect.size.width, height:texture!.size.height)

UIGraphicsBeginImageContextWithOptions(contextRect.size, false, 1.0)

for i in 0..<numOfHorTiles {

let context = UIGraphicsGetCurrentContext()

if (i % 2 == 0) {

CGContextTranslateCTM(context, texture!.size.width, 0)

CGContextScaleCTM(context, -1.0, 1.0)

}

texture!.drawInRect(CGRect(x: CGFloat(i)*texture!.size.width, y: 0, width: texture!.size.width, height: texture!.size.height))

}

newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return SKTexture(image: newImage)

}



Thats my function. Having some issues with the x-flipping, some of the flipped images are not displayed in the final image

Well, you're flipping the entire context from left to right. That means your even- and odd-numbered draws are happening at opposite ends of the context, probably not what you intended. I'm also not sure if repeating the sequence of transforms restore the original conditions.


You'll probably have better results if you draw all the odd-numbered images first, then flip the context once, then draw all the even-numbered images, offset to fill in the gaps. However, that's a drawing question, not what you asked about. (The point about flipping in my example code is that sometimes you're dealing with CGContext coordinates, with y==0 at the bottom, and sometimes with UIKit coordinates, with y==0 at the top. You need to flip the context vertically if you have the "wrong" coordinates.)


I also have to ask why you want to write things like 'texture!.size.width', when you can more easily (and more efficiently) do this:


let texture = UIImage(named: "bolt_1")! // crash now if the image doesn't exist, instead of later


and then say more naturally 'texture.size.width'.

How do you merge a sequence of images into one texture
 
 
Q