0

我有一个应用程序,我正在用手指做签名,它工作正常,它画线,但我想将这些线绘制的签名保存为 ImageView 中的图像以显示屏幕,但它没有显示我正在使用以下代码。

-(void)onSignButtonClick
{

    signatureView=[[UIView alloc] initWithFrame:CGRectMake(100,100,800,500)];

    signatureView.backgroundColor=[UIColor blackColor];

    [self.view addSubview:signatureView];

    UIButton*OkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [OkButton setFrame:CGRectMake(100,420,200,40)];
    [OkButton setTitle:@"OK" forState:UIControlStateNormal];
    [OkButton addTarget:self action:@selector(onOKButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [signatureView addSubview:OkButton];

    drawScreen=[[MyLineDrawingView alloc]initWithFrame:CGRectMake(10,10,700,400)];
    [signatureView addSubview:drawScreen];
    [drawScreen release];
}


- (UIImage *)captureView {

    //hide controls if needed
        CGRect rect = [signatureView bounds];//use your signature view's Rect means Frame;
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.view.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
}

-(void)onOKButtonClick{


    signatureView.hidden=YES;

    [self captureView];

      logoImageView.image=signatureImage;
}
4

3 回答 3

0

更换你的

logoImageView.image=signatureImage;

logoImageView.image = [self captureView];

编辑

然后替换

[self.view.layer renderInContext:context];

在你的captureView方法中:

[self.signatureView.layer renderInContext:context]; 
于 2013-05-16T06:56:05.973 回答
0

在您的方法 captureView 中使用它。这对我有用

**UIGraphicsBeginImageContext(tempV.frame.size);
 [[tempV layer] renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();**

并像这样改变你的功能

-(void)onOKButtonClick{
signatureView.hidden=YEs;

    logoImageView.image=[self captureView];

}
于 2013-05-16T06:56:55.597 回答
0

尝试这个:

-(void)onOKButtonClick {
    //first capture image from view
    CGRect rect = [signatureView bounds];   //use your signature view's Rect means Frame;

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [signatureView.layer renderInContext:context];   //this line is also important for you
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //take some part of image (crop image)
    UIImage *imgFinal = [self cropImage:img rect:CGRectMake(100,100,300,300)];
    logoImageView.image = imgFinal;

    signatureView.hidden=YES;
}

-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect {
   CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
   UIImage *img = [UIImage imageWithCGImage:imageRef]; 
   CGImageRelease(imageRef);
   return img;
}

出现问题是因为您的 captureView 方法在定义中返回 UIImage 但您不能将其存储在方法调用中......

于 2013-05-16T06:58:02.590 回答