7

我有UIViewwith backgroundColorset with colorWithPatternImage。正如预期的那样,背景图像从左上角开始绘制。

当我renderInContext在该视图上进行操作时出现问题:背景图像是从左下角开始绘制。其他一切似乎都很好。

这是源图像和目标图像:

在此处输入图像描述

这是代码:

// here is the layer to be rendered into an image
UIView *src = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, {100, 100}}];
src.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:src];

// here we'll display the image
UIImageView *dest = [[UIImageView alloc] initWithFrame:(CGRect){{110, 0}, src.bounds.size}];
[self.view addSubview:dest];

// render `src` to an image in `dest`
UIGraphicsBeginImageContext(src.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[src.layer renderInContext:context];
dest.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

有没有办法让图像以正确的方向平铺,就像在src视图中一样?

4

2 回答 2

6

我设法通过调用高度为 modf(viewHeight, patternHeight) 的 CGContextSetPatternPhase 来解决这个问题

于 2012-12-03T11:19:35.130 回答
2

https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066-CH202-CJBBAEEC

该文档解释了为什么会发生这种情况,特别是这一部分:

重要提示:如果您打算在 iOS 上编写直接针对 Quartz 的应用程序,上述讨论对于理解这些内容是必不可少的,但这还不够。在 iOS 3.2 及更高版本中,当 UIKit 为您的应用程序创建绘图上下文时,它还会对上下文进行额外的更改以匹配默认的 UIKIt 约定。特别是,不受 CTM 影响的图案和阴影被单独调整,以便它们的约定与 UIKit 的坐标系相匹配。在这种情况下,没有与 CTM 等效的机制,您的应用程序可以使用它来更改 Quartz 创建的上下文以匹配 UIKit 提供的上下文的行为;您的应用程序必须识别它正在绘制的上下文类型并调整其行为以匹配上下文的期望。

于 2013-04-04T11:51:09.320 回答