ios - renderInContext 翻转 colorWithPatternImage 的原点

标签 ios

我有 UIViewbackgroundColor 设置为 colorWithPatternImage。正如预期的那样,背景图像是从左角开始绘制的。

当我在该 View 上执行 renderInContext 时出现问题:背景图像是从 bottom 左角开始绘制的。其他一切似乎都很好。

这是源图像和目标图像:

enter image description here

代码如下:

// 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/

相关文章:

iphone - kSecReturnPersistentRef 有什么作用?

ios - 如何将 TableView Controller 添加到基于选项卡的应用程序 Xcode 4.3.3

ios - 如何检测用户在 iOS 中使用了语音输入?

ios - 在运行时从 View Controller 更改 UI

c# - 如何从 Xamarin iOS 调用 sysctl?

ios - 在 ios 中异步调用 Web 服务

ios - 将父类作为目标添加到 UIButton

iphone - UITableViewCell 附件在滚动到屏幕外时消失

ios - 也将 Parse iOS SDK 用于网络应用程序

javascript - html音频在Macbook和ios上不起作用,但在imac上起作用—很奇怪