2012-11-20 57 views
0

Signature.h文件如何cordinates轉換爲圖像

#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 

Signature.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文件本身中找到它們。請指出我的錯誤並糾正我。

+0

爲什麼你註釋掉// self.image = UIGraphicsGetImageFromCurrentImageContext(); ? – jimpic

回答

1

我真的不理解你的問題,但這個代碼錯誤的原因是在觸摸部分。

如果你想畫,而你碰,那麼你必須從你想在繪製視圖中使用drawInRect方法。

如果你想畫過的圖像保存或用戶釋放後,目前它那麼你必須在touchesBegan上創建圖像上下文,然後在touchesMoved上畫上它,最後將結果保存在touchesEnded

您可以提取的結果爲touchesEnded

UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

你的代碼是正確的,現在做的是,每次你移動你的手指時,你創建一個新的畫布,畫它,破壞它。當你擡起手指時,你會創建一個畫布並將其摧毀。

但我很確定你想移動你的手指並直接在屏幕上顯示結果。爲此,我真的建議你谷歌的教程,但基本上你必須:在觸摸開始創建一個點的數組,觸摸移動你添加點到這個數組,並在觸摸結束您添加到陣列的最後一點。 MEANWHILE關於drawInRect的視圖,你的代碼必須連續繪製這個數組中的點。

編輯:

假設你有一個有效的UIImage在年初 self.drawSign.image(空白畫布已經初始化)

嘗試:

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

我不是當然,如果這會起作用,但即使它這樣做也是一種可怕的方式(性能明智)。

+0

代碼無法正常工作。請提供一個很好的方法,如果可能的話,(性能明智)以及有用的資源。 – onkar

+0

@onkar http://rahulvirja.blogspot.in/2012/05/how-to-draw-line-in-iphone-sdk.html – user1536580