Using CGContext to display a sprite sheet iPhone

半城伤御伤魂 提交于 2019-11-27 14:15:19

问题


So I have a spritesheet in png format, and have already worked out the coordinates for what I want to display. I'm trying to create a method that will return a UIImage when passed the location information on the spritesheet. I'm just not sure how to use the CGContext stuff along with the the coordinates to return an UIImage.

I was looking to CGContextClipToRect because I think that is the way to do it, but I just can't seem to get it to work.

Something like this

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

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

Any help would be appreciated.

Thanks, Mike


回答1:


I think the call you want to use is CGImageCreateWithImageInRect

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



回答2:


you need to think of your co-ordinates backwards. The CGSize is fixed. Translate your xPos,yPos so that your sprite appears under the window.



来源:https://stackoverflow.com/questions/2863626/using-cgcontext-to-display-a-sprite-sheet-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!