Using CGContext to Display iPhone Sprite Sheet

So, I have a spritesheet in png format and I have already worked out the coordinates for what I want to display. I am trying to create a method that will return a UIImage when passing location information on a sprite. I just don't know how to use the CGContext stuff along with the coordinates to return the UIImage.

I was looking for CGContextClipToRect because I think this is the way to do it, but I just cannot get it to work.

Something like that

CGSize size = CGSizeMake(xSize, ySize);
UIGraphicsBeginImageContext(size);
//CGContextClipToRect(spriteSheet.size, imageRect);
[spriteSheet drawAtPoint:CGPointMake(xPos, yPos)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;


only returns what is in the Context size. I need to be able to "move" this size box around the sprite if that makes sense.

Any help would be appreciated.

Thanks, Mike

+2


a source to share


2 answers


I think the call you want to use is CGImageCreateWithImageInRect



newImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect( [spriteSheet CGImage] , imageRect )];

      

+9


a source


you need to think about your coordinates back. CGSize is fixed. Translate xPos, yPos so that your sprite appears under the window.



0


a source







All Articles