使用CGContext显示精灵表iPhone [英] Using CGContext to display a sprite sheet iPhone
问题描述
所以我有一个png格式的spritesheet,并且已经计算了我想要显示的坐标。我正在尝试创建一个方法,在spritesheet上传递位置信息时将返回UIImage。我只是不确定如何使用CGContext的东西以及坐标来返回UIImage。
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.
我当时正在寻找CGContextClipToRect,因为我觉得这样做是可行的,但我似乎无法让它发挥作用。
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.
这样的东西
CGSize size = CGSizeMake(xSize,ySize);
UIGraphicsBeginImageContext(size);
//CGContextClipToRect(spriteSheet.size,imageRect);
[spriteSheet drawAtPoint:CGPointMake(xPos,yPos)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回newImage;
仅返回大小上下文中的内容。如果有意义的话,我需要能够在spritesheet周围移动这个大小的窗口。
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.
谢谢,
Mike
Thanks, Mike
推荐答案
我认为您要使用的电话是 CGImageCreateWithImageInRect
I think the call you want to use is CGImageCreateWithImageInRect
newImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect( [spriteSheet CGImage] , imageRect )];
这篇关于使用CGContext显示精灵表iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!