【问题标题】:Draw on a CGContext from a CGImage从 CGImage 在 CGContext 上绘制
【发布时间】:2011-10-16 02:47:08
【问题描述】:

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

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

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

但这并没有考虑到我只想绘制源图像的CGRect 的情况。我必须做一些技巧来完成这项工作吗?

【问题讨论】:

    标签: ios cocoa-touch image quartz-graphics


    【解决方案1】:

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

    CGImageCreateWithImageInRect(CGImage image, CGRect rect)
    

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

    【讨论】:

    • 请注意,结果会保留原始图像,虽然它比将图像绘制到上下文中更快,但它可能会消耗更多内存。
    【解决方案2】:

    这是来自 Apple Quartz 2D Programming Guide 的清单 11-1:

    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);
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多