我的应用程序有一个 ViewController,其中包含一个 UIView (CanvasView),您可以在其中绘制您的签名(我添加了一个标签“Hi Little John”来区分它),我还在它下面添加了一个 UIImage,用于通过触摸相机进行捕获按钮
现在,当我触摸相机按钮时,它只捕获 UIView 和 UILabel 而不是它显示的签名
我有两个类:我的 UIView 类 (CanvasView) 和我的 UIViewController,在我的 CanvasView 中我有这个截图代码:
@implementation CanvasView
-(UIImage *)getCanvasScreenshot{
//first it makes an UIImage from the view
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//now it will position the image, X/Y away from top left corner to get the portion required
UIGraphicsBeginImageContext(self.frame.size);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
//some other code goes here...
@end
在我的 ViewController 类中,我有一个触发事件的 IBAction
- (IBAction)captureSignature:(id)sender {
self.imageFirma.image = [canvasView getCanvasScreenshot];
}
我想在我的照片上捕捉签名任何帮助我将不胜感激
提前致谢
***** 编辑** 渲染签名的代码也在CanvasView中,它是三种方法
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
如果触摸是,则取决于事件:
- 一开始(touchesBegan)
- 如果它在移动(touchesMoved)
- 或者如果完成(touchesEnded)
为了渲染笔画,它们都调用了一个名为:
- (void) addLineToAndRenderStroke:(SmoothStroke*)currentStroke toPoint:(CGPoint)end toWidth:(CGFloat)width toColor:(UIColor*)color
如果你愿意,我可以更深入,但它有点长,因为它是一个在画布上进行描边的框架,所有这些方法的代码是:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(![JotStylusManager sharedInstance].enabled){
for (UITouch *touch in touches) {
[self addLineToAndRenderStroke:[self getStrokeForTouchHash:touch.hash]
toPoint:[touch locationInView:self]
toWidth:[self widthForPressure:JOT_MIN_PRESSURE]
toColor:[self colorForPressure:JOT_MIN_PRESSURE]];
}
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if(![JotStylusManager sharedInstance].enabled){
for (UITouch *touch in touches) {
// check for other brands of stylus,
// or process non-Jot touches
//
// for this example, we'll simply draw every touch if
// the jot sdk is not enabled
[self addLineToAndRenderStroke:[self getStrokeForTouchHash:touch.hash]
toPoint:[touch locationInView:self]
toWidth:[self widthForPressure:JOT_MIN_PRESSURE]
toColor:[self colorForPressure:JOT_MIN_PRESSURE]];
}
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if(![JotStylusManager sharedInstance].enabled){
for(UITouch* touch in touches){
SmoothStroke* currentStroke = [self getStrokeForTouchHash:touch.hash];
// now line to the end of the stroke
[self addLineToAndRenderStroke:currentStroke
toPoint:[touch locationInView:self]
toWidth:[self widthForPressure:JOT_MIN_PRESSURE]
toColor:[self colorForPressure:JOT_MIN_PRESSURE]];
// this stroke is now finished, so add it to our completed strokes stack
// and remove it from the current strokes, and reset our undo state if any
[_stackOfStrokes addObject:currentStroke];
[currentStrokes removeObjectForKey:@([touch hash])];
[stackOfUndoneStrokes removeAllObjects];
}
}
}
- (void) addLineToAndRenderStroke:(SmoothStroke*)currentStroke toPoint:(CGPoint)end toWidth:(CGFloat)width toColor:(UIColor*)color{
// fetch the current and previous elements
// of the stroke. these will help us
// step over their length for drawing
AbstractBezierPathElement* previousElement = [currentStroke.segments lastObject];
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
end.y = self.bounds.size.height - end.y;
if(![currentStroke addPoint:end withWidth:width andColor:color]) return;
//
// ok, now we have the current + previous stroke segment
// so let's set to drawing it!
[self renderElement:[currentStroke.segments lastObject] fromPreviousElement:previousElement includeOpenGLPrep:YES];
}