从 CGImage 绘制 CGContext

发布于 2024-12-10 16:45:38 字数 424 浏览 0 评论 0原文

我想使用 CGImage 中的图块在 CGContext 上进行绘制,我想按指定的顺序绘制。

根据文档这里似乎在图形上下文中绘制图像的唯一方法是

void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);

但这没有考虑到我只想绘制源图像的 CGRect 的情况。我需要采取一些技巧才能使这项工作成功吗?

I would like to draw on a CGContext by using tiles from CGImage that I would like to draw in a specified order.

According to documentation here it seems that the only method to draw an image in a graphic context is

void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);

But this doesn't take in account the case in which I want to draw just a CGRect of the source image. Do I have to do some tricks to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-12-17 16:45:38

如果我正确理解你的问题,那么你要求在特定的 CGRect 处裁剪图像,如果是这样,那么你需要看看这个方法。

CGImageCreateWithImageInRect(CGImage image, CGRect rect)

这将返回您一个 CGImageRef ,根据您的需要,我认为这就是您正在寻找的。

If I understand your question correctly then you are asking to crop an image at a particular CGRect, if so then you need to take a look at this method.

CGImageCreateWithImageInRect(CGImage image, CGRect rect)

This will return you a CGImageRef and according to your need I think this is what you are looking for.

梦幻的味道 2024-12-17 16:45:38

这是 Apple 的 Quartz 2D 编程指南

myImageArea = CGRectMake (rooster_head_x_origin, rooster_head_y_origin,
                            myWidth, myHeight);
mySubimage = CGImageCreateWithImageInRect (myRoosterImage, myImageArea);
myRect = CGRectMake(0, 0, myWidth*2, myHeight*2);
CGContextDrawImage(context, myRect, mySubimage);

请注意,CGImageCreateWithImageInRect 返回一个由您负责释放的引用,因此您需要在完成 mySubimage 后执行此操作代码>:

CGImageRelease(mySubimage);

This is listing 11-1 from Apple's Quartz 2D Programming Guide:

myImageArea = CGRectMake (rooster_head_x_origin, rooster_head_y_origin,
                            myWidth, myHeight);
mySubimage = CGImageCreateWithImageInRect (myRoosterImage, myImageArea);
myRect = CGRectMake(0, 0, myWidth*2, myHeight*2);
CGContextDrawImage(context, myRect, mySubimage);

Note that CGImageCreateWithImageInRect returns a reference that you are responsible for releasing, so you'll need to do this when you're done with mySubimage:

CGImageRelease(mySubimage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文