【问题标题】:how to convert UIView part as UIImage in iphone application如何在 iphone 应用程序中将 UIView 部分转换为 UIImage
【发布时间】:2013-05-14 08:10:56
【问题描述】:

我有一个应用程序,我用手指从用户那里得到签名,它工作正常,但我希望签名应该转换成图像并应该显示在 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];

【问题讨论】:

    标签: iphone image ipad signature


    【解决方案1】:

    尝试获取 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 保留为实例变量。

    【讨论】:

    • 如何调用该方法以及如何在imageView中显示该图片
    • 您应该从放置绘图视图的视图控制器中调用此方法;您可以将实例变量保留到绘图视图以供以后访问。对于 uiimageview 只需设置 yourImageView.image=signatureImage;
    • 查看我的编辑,您必须在视图控制器中调用这些方法。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2018-01-09
    • 2012-12-25
    相关资源
    最近更新 更多