背景
我正在使用Chapter 8,Example 14 —Resize and Rotate的Erica Saduns Cookbook示例显然调整大小并旋转UIImageView.
更新层次结构
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 768×768.
4.) above and below 3,are 2 views 128 in height.
******See Photo example below****
问题
目前,我可以使用[[[self view] layer] renderInContext :,但是,我需要一种方法来保存一个仅包含#2和#3(包括#2的转换)的768×768(照片示例中的绿绿色)框.如果我使用[[#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中概述的部分图像,同时保留用户的转换.
解决方法
如果我正确理解你的问题,那么你想要仅仅渲染第二层,但是层#2具有在渲染该层时不保留的变换?在将图层渲染到该上下文之前,可以将这些变换应用于图形上下文的CTM(当前转换矩阵).这样的事情应该有效:
CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGContextConcatCTM(ctx,[layer2 affineTransform]); [layer2 renderInContext:ctx]; CGContextRestoreGState(ctx);
请注意,只有在绘制图层之后,在上下文中绘制更多的东西,才需要调用CGContextSaveGState()和CGContextRestoreGState().如果图层是您想要的,您可以省略它们.