0

我有一个应用程序,我用手指从用户那里得到签名,它工作正常,但我希望签名应该转换成图像,并且应该在 imageview 中显示这里是我的签名编码的代码。

签名工作正常,它显示在视图上,但我想将这些图形线转换为图像并显示在 imageview 中。

   #import <UIKit/UIKit.h>
   @interface MyLineDrawingView : UIView {

  UIBezierPath *myPath;
  UIColor *brushPattern;
  }

  @end

实现类

   #import "MyLineDrawingView.h"
   @implementation MyLineDrawingView

  - (id)initWithFrame:(CGRect)frame
  {

 self = [super initWithFrame:frame];
 if (self) {
    // Initialization code

    self.backgroundColor=[UIColor whiteColor];
    myPath=[[UIBezierPath alloc]init];
    myPath.lineCapStyle=kCGLineCapRound;
    myPath.miterLimit=0;
    myPath.lineWidth=10;
    brushPattern=[UIColor redColor];
    }
    return self;
    }

// 仅覆盖 drawRect: 如果您执行自定义绘图。// 空实现会对动画期间的性能产生不利影响。

- (void)drawRect:(CGRect)rect
{
 [brushPattern setStroke];
 [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
 // Drawing code
 //[myPath stroke];
}

pragma mark - 触摸方法

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {

 UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
 [myPath moveToPoint:[mytouch locationInView:self]];

 }

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];

}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


 }

 - (void)dealloc
 { 

 [brushPattern release];
 [super dealloc];

 }

 @end

这就是我如何称呼这个观点

     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];

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

1 回答 1

0

尝试获取 MyLineDrawingView 的屏幕截图:

- (UIImage*)takeSignatureImage{
  UIGraphicsBeginImageContext(drawScreen.bounds.size);    
  [drawScreen.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();  
  return signatureImage;
}

-(IBAction)onOKButtonClick{
   UIImage *signatureImg=[self takeSignatureIamge];
   if(signatureImg){
      yourImageView.image=signatureImg;
   }
}

确保保留drawScreen为实例变量。

于 2013-05-14T09:30:35.217 回答