简体   繁体   English

如何在iPhone应用程序中将UIView部件转换为UIImage

[英]how to convert UIView part as UIImage in iphone application

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. 我有一个应用程序,在该应用程序中,我正在用手指从用户那里获得签名,但效果很好,但我希望将签名转换为图像,并在imageview中显示,这是我的签名代码。

Signature is working fine and it shows on the view but i want to convert those graphics line into image and display in imageview. 签名工作正常,它显示在视图上,但我想将这些图形行转换为图像并在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. //仅重写drawRect:如果执行自定义绘制。 // 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];

Try by getting a screenshot of your MyLineDrawingView: 尝试获取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. 确保将drawScreen保留为实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM