如何在Uphone应用程序中将UIView部分转换为UIImage

时间:2013-05-14 08:10:57

标签: iphone image ipad signature

我有一个应用程序,其中我用手指从用户获得它的工作正常,但我希望该签名应该转换为图像,并且应该在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];

1 个答案:

答案 0 :(得分: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保留为实例变量。