使用 CGContext 显示 iPhone 的精灵表
所以我有一个png格式的精灵表,并且已经计算出我想要显示的坐标。我正在尝试创建一个方法,当传递 spritesheet 上的位置信息时,该方法将返回 UIImage。我只是不确定如何使用 CGContext 内容以及坐标来返回 UIImage。
我一直在寻找 CGContextClipToRect 因为我认为这是做到这一点的方法,但我似乎无法让它工作。
像这样的
<代码> CGSize 大小 = CGSizeMake(xSize, ySize);
UIGraphicsBeginImageContext(大小);
//CGContextClipToRect(spriteSheet.size, imageRect);
[spriteSheet drawAtPoint:CGPointMake(xPos, yPos)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回新图像;
仅返回 size 上下文中的内容。如果有意义的话,我需要能够在精灵表周围“移动”这个大小的窗口。
任何帮助将不胜感激。
谢谢, 麦克风
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您要使用的调用是 CGImageCreateWithImageInRect
I think the call you want to use is
CGImageCreateWithImageInRect
你需要向后思考你的坐标。 CGSize 是固定的。平移您的 xPos、yPos,以便您的精灵出现在窗口下方。
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.