2011-07-21 8 views
6

Esiste un framework che posso utilizzare per disegnare linee con il tocco. Fondamentalmente voglio aggiungere la possibilità per un cliente di firmare su iPad/iPhone e catturarlo come immagine.C'è un SDK per disegnare linee su iOS con un tocco?

Qualsiasi aiuto molto apprezzato.

Grazie.

+0

possibile duplicato di http://stackoverflow.com/questions/679983/how-to-draw-signature-with-lines-on-iphone-screen – visakh7

risposta

10

è possibile soddisfare le proprie esigenze utilizzando la grafica di base disponibile nel framework UIKIT.

ho avuto un requisito simile nella mia applicazione con un uso diverso, se necessario posso fornire il codice.

TNQ

file h

#import <UIKit/UIKit.h> 
typedef enum _DrawingMode{ 
DrawingModePen =0, 
DrawingModeEraser=1, 
} DrawingMode; 
@interface DrawingView : UIView { 
CGPoint lastPoint; 
UIImageView *drawImage; 
BOOL mouseSwiped; 
int mouseMoved; 
DrawingMode mode; 
UIColor *_drawingPenColor; 
} 
@property (nonatomic, retain) UIColor *drawingPenColor; 
@property (nonatomic) DrawingMode mode; 

@property (nonatomic, retain) UIImageView *imageView; 
@property (nonatomic,retain)UIImageView *drawImage; 
@end 

.m

#import "DrawingView.h" 


@implementation DrawingView 

@synthesize mode, drawingPenColor=_drawingPenColor, imageView=drawImage; 


-(void)initialize{ 
    drawImage = [[UIImageView alloc] initWithImage:nil]; 
    self.autoresizesSubviews = YES; 
    drawImage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    drawImage.frame = self.bounds; 
    [self addSubview:drawImage]; 
    self.backgroundColor = [UIColor clearColor]; 
    mouseMoved = 0; 
    _drawingPenColor = [[UIColor alloc] initWithWhite:0.0 alpha:1.0]; 
} 

-(void)maskImage:(UIImage *)image withMask:(UIImage *)maskImage 
{ 
    CGImageRef maskRef = maskImage.CGImage; 
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); 
    UIImage *tempImage = [[UIImage alloc] initWithCGImage:masked]; 
    //self.clippedImage = tempImage; 
    [tempImage release]; 
    CFRelease(masked); 
    CFRelease(mask); 
} 

-(void)awakeFromNib{ 
    [self initialize]; 
} 

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     // Initialization code 
     [self initialize]; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     drawImage.image = nil; 
     return; 
    } 

    lastPoint = [touch locationInView:self]; 
    //lastPoint.y -= 20; 
    NSLog(@"touches begin"); 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 
    //currentPoint.y -= 20; 


    UIGraphicsBeginImageContext(self.bounds.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    if (mode == DrawingModePen) { 
     NSLog(@"drawing"); 
     CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]); 
    } 
    else { 
     CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]); 

     CGContextBeginPath(UIGraphicsGetCurrentContext()); 


     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 20, 20)); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 

     NSLog(@"clearing"); 
      } 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

    mouseMoved++; 

    if (mouseMoved == 10) { 
     mouseMoved = 0; 
    } 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     drawImage.image = nil; 
     return; 
    } 


    if(!mouseSwiped) { 
     UIGraphicsBeginImageContext(self.bounds.size); 
     [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     if (mode == DrawingModePen) { 
      CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]); 
     } 
     else { 
      CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [self.backgroundColor CGColor]); 
     } 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

- (void)dealloc { 
    [drawImage release]; 
    [_drawingPenColor release]; 
    [super dealloc]; 
} 

@end 
+0

cool ... puoi fornire il codice? – Dev

+0

non so fino a che punto ti aiuterà, ma puoi provare nel tuo caso se una persona disegna una linea a passo lento e usando un po 'più di spazio sul mio codice. – Dinakar

+1

fantastico ... questo aiuta davvero ... giocherà di più con esso ... grazie mille. – Dev

0

V'è ora un framework chiamato BrushEngine che potete trovare a http://www.cdframeworks.com/product/brushengine

BrushEngine consente di disegnare con le tue dita usando linee, forme (vari built-in), immagini personalizzate e molti altri. È quindi possibile salvare l'output come immagine o video.

Problemi correlati