尝试在iOS中屏幕捕获UIView

时间:2013-10-09 19:16:33

标签: ios objective-c uiview screenshot jot-touch-sdk

我的应用程序有一个ViewController,里面包含一个UIView(CanvasView),你可以在其中绘制你的签名(我添加了一个标签“Hi Little John”来区分它),我还在它下面添加了一个UIImage来进行捕获触摸相机按钮

enter image description here

现在,当我触摸相机按钮时,它仅捕获UIView和UILabel,但不会显示签名

enter image description here

我有两个类:我的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 *)触及withEvent:(UIEvent *)事件
  • (void)touchesMoved:(NSSet *)触及withEvent:(UIEvent *)事件
  • (void)touchesEnded:(NSSet *)触及withEvent:(UIEvent *)事件

取决于触摸的事件:

  • 在开始时(touchesBegan)
  • 如果它正在移动(touchesMoved)
  • 或if if finished(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];
}

0 个答案:

没有答案