如何在Uphone应用程序中将UIView部分转换为UIImage [英] how to convert UIView part as UIImage in iphone application

查看:117
本文介绍了如何在Uphone应用程序中将UIView部分转换为UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

   #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:如果执行自定义绘图。
//空实现会对动画期间的性能产生负面影响。

// 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 - 触摸方法



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


推荐答案

尝试获取MyLineDrawingView的屏幕截图:

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

确保保持 drawScreen 作为实例变量。

An make sure to keep drawScreen as instance variable.

这篇关于如何在Uphone应用程序中将UIView部分转换为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆