CATiledLayer 或 CALayer 绘制 UIImage 不起作用
大家好;
我正在尝试手动(不使用子图层)在 CATiledLayer 中绘制图像,但它的行为与定义的解决方案不符。我知道,当您调用“CGContextDrawImage”时,您必须缩放和平移以便翻转它,但我一生都无法让它工作。
我有一个名为 的方法
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image
,从 in 中调用该方法多次,
- (void)drawInContext:(CGContextRef)context
以渲染进入 CATileLayer 的所有图像。
以下不渲染图像:
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, image.CGImage);
CGContextRestoreGState(context);
以下产生图像但渲染不正确:
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextDrawImage(context, rect, image.CGImage);
同样,
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, image.CGImage);
我也尝试过:
UIGraphicsBeginImageContext
UIGraphicsEndImageContext
和
UIGraphicsPushContext
UIGraphicsPopContext
全部不起作用。我在这里遗漏了一些基本的东西,我怀疑我保存上下文的方法不起作用。
Good day all;
I am trying to manually (not using sublayers) draw images within a CATiledLayer but it is not behaving as it should with the defined solution. I undertand that when you call 'CGContextDrawImage' you must scale and translate so as to flip it but I cannot for the life of me, get it to work.
I have a method called
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image
which is called several times from with in
- (void)drawInContext:(CGContextRef)context
to render all the images that go into the CATileLayer.
The following renders no images:
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, image.CGImage);
CGContextRestoreGState(context);
The following produces an image but rendered incorrectly:
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextDrawImage(context, rect, image.CGImage);
Like wise with this:
- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, image.CGImage);
I have also tried:
UIGraphicsBeginImageContext
UIGraphicsEndImageContext
and
UIGraphicsPushContext
UIGraphicsPopContext
All do not work. I am missing something fundamental here and I suspect my approach to saving the contexts is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我知道这里发生了什么;
看起来图像实际上正在渲染,但渲染在屏幕之外。翻转不是相对于层的原点发生的,而是相对于超级层的原点发生的。
我看到的问题是我有一个大层和一个大的子层网格。我假设子层内的翻转将与其自身的坐标相关,但事实并非如此。
OK I think I know what is happening here;
It appears that the image is actually rendering but rendering off screen. The flipping is not happening relative to the origin of the layer but rather the origin of the super layer.
The issue I am seeing is that I have a large layer and a large grid of sublayers. I assumed that a flip within the sublayer, would be relative to it's own co-orindates but that is not the case.