我有 UIView
,backgroundColor
设置为 colorWithPatternImage
。正如预期的那样,背景图像是从上左角开始绘制的。
当我在该 View 上执行 renderInContext
时出现问题:背景图像是从 bottom 左角开始绘制的。其他一切似乎都很好。
这是源图像和目标图像:
代码如下:
// 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
View 中那样使图像在正确的方向平铺?
最佳答案
我通过调用高度为 modf(viewHeight, patternHeight) 的 CGContextSetPatternPhase 设法解决了这个问题
关于ios - renderInContext 翻转 colorWithPatternImage 的原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13445850/