动态缩放 CGPatternRef
嘿,我有一个视图,其中包含我在其中放置的一些项目,以及平铺的背景图像。我已将其背景颜色设置为 CGPatternRef。我正在尝试获取它,以便可以将背景图像与图层中的内容一起缩放。我的背景图像缩放得很好,我只是似乎不知道如何在比例变化时更新它。这是我的drawCallback,
static void drawPatternImage (void *info, CGContextRef ctx)
{
CGImageRef image = (CGImageRef) info;
CGContextDrawTiledImage(ctx, CGRectMake(0,0, CGImageGetWidth(image)*scaleValue, CGImageGetHeight(image)*scaleValue), image);
}
图像是按照我期望的方式绘制的,但是当我更改比例值,然后在我的图层上调用setNeedsDisplay时,不会再次调用回调方法,并且我的背景也不会更新。这是解决这个问题的最佳方法吗?或者在我看来,有没有更有效的方法来绘制可缩放的可平铺图像。我实现了 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 并从那里调用 CGContextDrawTiledImage,但是在缩放视图时,更新非常不稳定。
任何指导将不胜感激。
谢谢
Hey, I've got a view with some items I lay out in it, and a tiled background image. I've set it's backgroundColor to a CGPatternRef. I am trying to get it so I can scale the background image in tandem with the contents in my layer. I've got the background image scaling fine, I just can't seem to figure out how to make it update when the scale changes. Here is my drawCallback
static void drawPatternImage (void *info, CGContextRef ctx)
{
CGImageRef image = (CGImageRef) info;
CGContextDrawTiledImage(ctx, CGRectMake(0,0, CGImageGetWidth(image)*scaleValue, CGImageGetHeight(image)*scaleValue), image);
}
the image is drawn how I expect, but when I change the scale value, and then call setNeedsDisplay on my layer, the callback method isn't called again, and my background isn't updated. Is this the best way to approach this problem? Or is there a more efficient way of drawing a scalable tileable image in my view. I implemented - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx and called CGContextDrawTiledImage from in there, but when scaling my view, the updating was very choppy.
Any guidance would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CGPattern 需要知道模式的大小,您可以将其传递给
CGPatternCreate
。仅在回调中绘制不同的尺寸是没有帮助的,因此您必须销毁并重新设置模式,将新尺寸传递给 CGPatternCreate 。The CGPattern needs to know the size of the pattern, you pass that in your call to
CGPatternCreate
. Just drawing in a different size in you callback isn't going to help, so you must destroy and re-setup the pattern, passing the new size toCGPatternCreate
.