我几乎逐字地使用了来自http://developer.apple.com/library/ios/#qa/qa1714/_index.html的示例代码,我可以保存我的背景图片或叠加层,但我无法将两者结合起来。当我尝试使用以下代码组合它们时,叠加层渲染背景为白色,我假设它覆盖了背景。知道为什么会这样吗?
这是我尝试将视图叠加层与图像组合的方法:
-(void) annotateStillImage
{
UIImage *image = stillImage;
// Create a graphics context with the target size
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw the image returned by the camera sample buffer into the context.
// Draw it into the same sized rectangle as the view that is displayed on the screen.
UIGraphicsPushContext(context);
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
UIGraphicsPopContext();
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [[self view] center].x, [[self view] center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [[self view] transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[[self view] bounds].size.width * [[[self view] layer] anchorPoint].x,
-[[self view] bounds].size.height * [[[self view] layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[[self view] layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
// Retrieve the screenshot image containing both the camera content and the overlay view
stillImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
以下是我的方法的一个片段,它会在相关的情况下创建视图:
CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer] setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"acceptButton.png"] forState:UIControlStateNormal];
[overlayButton setFrame:[self rectForAcceptButton] ];
[overlayButton setAutoresizesSubviews:TRUE];
[overlayButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin];
[overlayButton addTarget:self action:@selector(acceptButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
我希望annotate方法中的图像是背景,并且视图中的accept按钮将添加到顶部。我得到的只是接受按钮和白色背景,但如果我不运行renderInContext方法,我得到的只是图像背景而没有叠加。
谢谢!
答案 0 :(得分:2)
原来我将叠加层添加到与预览图层相同的视图中,而不是将其放在自己的视图中。我通过回顾这个涵盖所有类型屏幕截图的伟大项目来实现这一点:https://github.com/cocoacoderorg/Screenshots
修复是在具有透明背景的IB中创建单独的叠加视图,并将我的叠加元素添加到此。