【发布时间】:2025-12-08 14:00:01
【问题描述】:
我几乎一字不差地使用来自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();
}
这是我的方法的一个 sn-p,它在相关的情况下创建视图:
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 方法中的图像是背景,视图绘图中的接受按钮将添加到顶部。我得到的只是接受按钮和白色背景,但如果我不运行 renderInContext 方法,我得到的只是图像背景,没有覆盖。
谢谢!
【问题讨论】:
标签: iphone cocoa-touch uiimage core-animation core-graphics