how to convert UIView part as UIImage in iphone ap

2019-01-29 12:24发布

问题:

I have a application in which i am getting sign from the user with finger it works fine but i want that signature should be converted into image and should display in imageview here is the code for my signature coding.

Signature is working fine and it shows on the view but i want to convert those graphics line into image and display in imageview.

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

  UIBezierPath *myPath;
  UIColor *brushPattern;
  }

  @end

Implementation Classs

   #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;
    }

// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation.

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

pragma mark - Touch Methods

  -(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

here is how i am calling this view

     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:

Try by getting a screenshot of your 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;
   }
}

An make sure to keep drawScreen as instance variable.