0

签名.h 文件

#import <UIKit/UIKit.h>

@interface Signature : UIViewController
{

    CGPoint firstTouch;
    CGPoint lastTouch;
    UIColor *pointColor;
    CGRect *points;
    int npoints;
    CGPoint location;
   // UIImageView *signatureImageView;
}
 @property CGPoint firstTouch;
@property CGPoint lastTouch;
@property (nonatomic, retain) UIColor *pointColor;
@property CGRect *points;
@property int npoints;

@property (retain, nonatomic) IBOutletCollection(UIImageView) NSArray *drawSign;
@property CGPoint location;

@property (retain, nonatomic) IBOutletCollection(UIImageView) NSArray *signatureImageView;
- (IBAction)savePressed:(id)sender;
- (IBAction)clearPressed:(id)sender;

@end

签名.m

#import "Signature.h"

@interface Signature ()

@end

@implementation Signature
@synthesize drawSign;
@synthesize signatureImageView;
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize  pointColor;
@synthesize  points;


- (id)initWithFrame:(CGRect)frame
{
    return self;
}


- (id)initWithCoder:(NSCoder *)coder
{
    if(self = [super initWithCoder:coder])
    {
        self.npoints = 0;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    NSLog(@"initwithnibname");
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"viewdidload");

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    NSLog(@"view did unload");

    [self setSignatureImageView:nil];
    [self setDrawSign:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touch has began");

    UITouch *touch = [touches anyObject];
    self.location = [touch locationInView:self.view];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches moves");

    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self.view];
   UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

  //[self.drawSign.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    NSLog(@"%f x is",location.x);
     NSLog(@"%f y is",location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
   // self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches ended");

    UITouch *touch = [touches anyObject];
  CGPoint currentLocation = [touch locationInView:self.view];

   UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //[self.view.image drawInRect:CGRectMake(0, 0, self.frame.view.size.width, self.frame.view.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
   CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
  //  self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [signatureImageView release];
    [super dealloc];
}
- (IBAction)savePressed:(id)sender {
    NSLog(@"save pressed");

}

- (IBAction)clearPressed:(id)sender {
    NSLog(@"cancel pressed");

}
@end

请找到与我的项目相关的两个文件。从一个观点我来到这个观点。现在我需要保存用户在上面绘制的任何内容。我能够获取坐标但无法绘制它们。我已经尝试了几次,但似乎都没有工作。请以注释方式在Signature.m文件本身中找到它们。请指出我的错误并纠正我。

4

1 回答 1

1

我真的不明白你的问题,但这段代码错误的原因在于接触部分。

如果你想在触摸的同时进行绘制,那么你必须在你想绘制的视图中使用 drawInRect 方法。

如果你想在用户释放图像后绘制图像以保存或呈现它,那么你必须在touchesBegan上创建一个图像上下文,然后在touchesMoved上绘制它,最后将结果保存在touchesEnded中。

您可以在touchesEnded处提取结果

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

您的代码现在正在做的是,每次您移动手指时,您都会创建一个新的画布,在其上绘制并销毁它。当您抬起手指时,您会在其上再创建一个画布并销毁它。

但我很确定您想移动手指并直接在屏幕上显示结果。为此,我真的建议你在谷歌上找到一个教程,但基本上你必须:在触摸开始时创建一个点数组,在触摸移动时向这个数组添加点,在触摸结束时将最后一个点添加到数组中。MEANWHILE 在视图 drawInRect 上,您的代码必须连续绘制此数组中的点。

编辑:

假设您在self.drawSign.image的开头有一个有效的 UIImage(已经初始化的空白画布)

尝试:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches moves");

    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self.view];
   UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [self.drawSign.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    NSLog(@"%f x is",location.x);
     NSLog(@"%f y is",location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.drawSign.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches ended");

    UITouch *touch = [touches anyObject];
  CGPoint currentLocation = [touch locationInView:self.view];

   UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.drawSign.image drawInRect:CGRectMake(0, 0, self.frame.view.size.width, self.frame.view.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
   CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.drawSign.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
} 

我不确定这是否可行,但即使这样做也是一种可怕的方式(性能方面)。

于 2012-11-20T08:12:00.143 回答