将转换后的图像层应用于 renderInContext:
背景
我正在使用来自 第 8 章示例 14 — 调整大小和旋转
明显地调整大小和旋转UIImageView
。
查看层次结构
1.) 条纹背景视图。
2.) 可以调整大小和旋转的交互式视图。
3.) 具有透明部分的覆盖图像。该视图的 y 轴从 128 开始,尺寸为 768x768。
4.) 3 的上方和下方是 2 个高度为 128 的视图。
*****参见下面的照片示例****
问题
目前,我可以使用 [[[self view] layer] renderInContext:
和 #2 的转换是正确的。但是,我需要一种方法来保存仅包含 #2 和 #3 的 768x768 (照片示例中的柠檬绿) 框架,包括#2的转换。如果我使用 [[#2 layer] renderInContext:
,我会得到原始图像,并且没有任何转换。 (参见下面的屏幕截图以获取参考。
代码
CGSize deviceSpec;
if ( IDIOM == IPAD ) { deviceSpec =CGSizeMake(768,768); } else { deviceSpec =CGSizeMake(320,480); }
if ( scale > 1.5 ) {
UIGraphicsBeginImageContextWithOptions(deviceSpec, NO, scale);
} else {
UIGraphicsBeginImageContext( deviceSpec );
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[stripedBg layer] renderInContext:ctx]; //#1
CGContextSaveGState(ctx);
CGContextConcatCTM(ctx, [[interactiveImage layer] affineTransform]);
//CGContextTranslateCTM(ctx, interactiveImage.frame.origin.x,interactiveImage.frame.origin.y-128);
[[interactiveImage layer] renderInContext:ctx]; // #2
CGContextRestoreGState(ctx);
[[overlayImage layer] renderInContext:ctx]; // #3
UIImage * draft = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
照片示例
我只需要以 LIME GREEN 概述的图像部分,同时保留用户进行的转换。
Background
I am using Erica Saduns Cookbook example from Chapter 8, Example 14 — Resize and Rotate
to obviously resize and rotate a UIImageView
.
VIew hierarchy
1.) striped background view.
2.) the interactive view which can be resize and rotate.
3.) an overlay image with a transparent portion. this view starts its y axis at 128 and is 768x768.
4.) above and below 3, are 2 views 128 in height.
******See Photo example below****
Issue
Currently, I can save the entire view's layer to the photo library using [[[self view] layer] renderInContext:
, and #2's transformations are correct. However, I need a way to save a 768x768 (lime green in photo example) frame that only includes #2 and #3, including #2's transformations. If I use [[#2 layer] renderInContext:
, I get the original image, and no transformations. (see screenshot below for # reference.
Code
CGSize deviceSpec;
if ( IDIOM == IPAD ) { deviceSpec =CGSizeMake(768,768); } else { deviceSpec =CGSizeMake(320,480); }
if ( scale > 1.5 ) {
UIGraphicsBeginImageContextWithOptions(deviceSpec, NO, scale);
} else {
UIGraphicsBeginImageContext( deviceSpec );
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[stripedBg layer] renderInContext:ctx]; //#1
CGContextSaveGState(ctx);
CGContextConcatCTM(ctx, [[interactiveImage layer] affineTransform]);
//CGContextTranslateCTM(ctx, interactiveImage.frame.origin.x,interactiveImage.frame.origin.y-128);
[[interactiveImage layer] renderInContext:ctx]; // #2
CGContextRestoreGState(ctx);
[[overlayImage layer] renderInContext:ctx]; // #3
UIImage * draft = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Photo Example
I only need the portion of the image that is outlined in LIME GREEN, while preserving the transformations by the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,问题是您只想渲染第 2 层,但第 2 层的变换在仅渲染该层时未保留?在将图层渲染到上下文中之前,您可以将这些变换应用到图形上下文的 CTM(当前变换矩阵)。像这样的东西应该可以工作:
注意,只有当您想在绘制图层后将更多内容绘制到上下文中时,才需要调用
CGContextSaveGState()
和CGContextRestoreGState()
。如果您想要图层,则可以忽略它们。If I understand you correctly, the issue is that you want to render just layer #2, but layer #2 has transforms that aren't preserved when rendering just that layer? You can apply those transforms to the graphics context's CTM (current transformation matrix) before you render the layer into that context. Something like this should work:
Note, the calls to
CGContextSaveGState()
andCGContextRestoreGState()
are only necessary if you want to draw more stuff into the context after you draw the layer. You can omit them if the layer is all you want.